A. Wait (), notify (), and Notifyall ()
To better support collaboration between multiple threads, the JDK provides three important local methods
//the Wait () method that invokes an object can cause the current thread to block, and the current thread must have a lock on the object. Public Final voidWait ()throwsinterruptedexception {Wait (0); }//invoking an object's Notify () method wakes up a thread that is waiting for the lock on the object, and if more than one thread waits for the lock on the object, only one of the threads can be awakened Public Final native voidnotify ();//the Notifyall () method wakes up all threads that are waiting for this object to lock; Public Final native voidNotifyall ();
When a thread with an object lock calls the Wait () method, it causes the current thread to join the object.wait wait queue and releases the currently occupied object lock so that other threads have a chance to acquire the object lock. The thread that obtains the object lock calls the Notify () method to wake up a thread at random in the object.wait wait queue (the wake is random regardless of the order in which it is added, the high-priority wake probability is high), and if the call to the Notifyall () method wakes up all the threads. Note: After calling the Notify () method, the object lock is not released immediately, and the object lock is released after the thread has finished executing.
Code:
Public classWaittest {Private StaticObject object=NewObject (); Public Static voidMain (string[] args) {thread thread=NewThread () {@Override Public voidrun () {synchronized(object) {System.out.println (System.currenttimemillis ()+ ":" +thread.currentthread (). GetName () + "Enter Start"); Try{object.wait ();//causes the current thread to enter the wait (into the object.wait queue) and releases the object lock}Catch(interruptedexception e) {e.printstacktrace (); } System.out.println (System.currenttimemillis ()+ ":" +thread.currentthread (). GetName () + "End of Thread execution"); } } }; Thread.Start (); Thread thread_2=NewThread () {@Override Public voidrun () {synchronized(object) {System.out.println (System.currenttimemillis ()+ ":" +thread.currentthread (). GetName () + "Enter Start"); Try{object.notify ();//randomly wakes a thread waiting for the object lock in the OBJECT.WAITD queueSystem.out.println (System.currenttimemillis () + ":" +thread.currentthread (). GetName () + "Wake up a Waiting thread"); Thread.Sleep (10000); } Catch(interruptedexception e) {e.printstacktrace (); } } } }; Thread_2.start (); }}
Execution Result:
1473306408730:thread-0 Enter start 1473306408731:thread-1 Enter start 1473306408731:thread-1 Wake up a waiting thread 1473306418731:thread-0 thread Execution ends
From the timestamp, it can be seen that the Thread-0 does not execute immediately after notifying Thread-0 to continue execution, but instead waits for the Thread-1 to release the object lock before Thread-1 the object lock again. (The last two timestamps subtract exactly 10 seconds)
Java Thread collaboration Wait (wait) with notiy (notification)