From version 1.0, J2SE provides WAIT, Notify, and NotifyAll. This article introduces these three methods and conducts certain analysis, and finally gives some questions, I hope to inspire everyone. Although the three methods mentioned above are the most often the thread, it does not mean that these methods are the member functions of the Thread class. In fact, Java fully considers synchronous problems when designing, and thus provides these three functions when designing basic class OJECT. This means that any classes you have written can use these methods (of course, these methods are meaningless). According to the above statement, you must have such a doubt: Since there is a relationship with the object, why is the most common use in the program I see? I will give this question to the answer! First, we must know that Wait () is absolutely different in nature. Sleep () makes a process into sleep state, but the resources occupied by the thread are not released. For example, you call Sleep () in the Synchronized module (plus function lock or object lock), although the thread is asleep and there is no resources, but it still saves locks, and other threads still cannot call the associated Synchronized module. Wait () is different. It actually gives up the lock and contributes resources, and makes you temporarily leave the post. This departure state has always continued until "boss" calls NOTIFY (), and the notification thread continues to work. If you are familiar with the program framework, you will definitely think that Wait () and Notify () have a huge role! ! ! If your program is not a toy, you may need to use these three functions. Most of the current programs are multi-threaded, different threads correspond to different functions. A good design, can use the user to increase the functionality and slimming of the software when the user is running. According to the stupid approach, we can use the While constantly judging a sign to identify if the user launches a feature. This way, the execution of the While statement occupies the cycle of the CPU, which reduces performance. If we use Wait (), when we need to use a feature, you only need to use Notify to "on".
How do we do it in Java, we have to know, but we can infer that JRE is definitely in the open space to store information, so that Notify finds an interrupt location, let thread continue to run! This is a typical spatial change time! ! ! It is worth mentioning that synchronization in the thread is the application of these three methods! I will give the procedures I have written in my own, explain how to use these three methods (absolutely original !!!): File 1: waitsyn.javaimport java.lang.Thread; import java.lang.interruptedException; public class Waitsyn Extends thread {public void run () {waitting ();} public synchronized void wait set ("Waitting"); Wait (); system.out.println ("bingle");} Catch (InterruptedException E) {E.PrintStackTrace ();}} public void callup () {synchronized (this) {system.out.println ("in the callup ()"); notifyAll ();}}} file 2: Callsyn.javaimport java.lang.thread;
public class CallSyn extends Thread {WaitSyn waitSyn; public CallSyn (WaitSyn input) {waitSyn = input;} public void run () {waitSyn.callup (); System.out.println ( "In the ClassSyn");}} File 3 : Test.javaimport java.lang.interruptedException;
Public class test {public static void main (string arg []) {waitsyn waitsyn = new waitsyn (); waitsyn.start (); callsyn calsyn = new callsyn (waitsyn); callsyn.start ();}}} The program is relatively simple, I don't explain, you should understand it. I have to perform the function when I have the function, and I have introduced it deeply. This is the experience of the younger, if there is a place, I hope everyone will make progress together! ! ! Last continued