Wait () and notify () are directly affiliated with the Object class, that is, all objects have this method. It seems that this is very incredible at first, but it is actually very natural, because this pair of methods need to release the occupied lock when blocking, and the lock is available in any object. Calling the WAIT () method of any object to cause thread blocking. And the lock on the object is released. The Notify () method of calling any object causes a randomly selected blocking block in a thread that is obstructed by the WAIT () method of the object (but it can be executed until it is locked).
Secondly, the Wait () and notify () can be called at any position, but this pair must be called in the Synchronized method or block. The reason is very simple. The lock can be released. In the same way, the lock on the object of this pair of methods must be owned by the current thread, so that the lock can be released. Therefore, the method call must be placed in such a Synchronized method or block. The lock object of this method or block is the object of the call. If this condition is not met, although the program can still be compiled, IllegalMonitorsStateException will appear during runtime.
The above features of the Wait () and notify () methods determine that they often use it with the Synchronized method or block. Compare them to the processing mechanism between the process system of the operating system, and they will find their similarity: the Synchronized method or block provides provides a method or block provides provides a method or block. Similar to the functions of operating systems, their execution will not be disturbed by multi -threaded mechanisms, and this pair is equivalent to Block and WakeUp primitives (this pair of methods all declare Synchronized). Their combination enables us to realize a series of exquisite inter -communication algorithms (such as signal quantity algorithms) on the operating system and use it to solve various complex thread inter -communication problems.
About the wait () and notify () methods finally explain two points:
First: Call the notify () method that causes the blocking thread to be randomly selected from the thread obstructed by the WAIT () method that calls the object. , Avoid problems due to this uncertainty.
Second: In addition to notify (), there is also a method notifyAll () that can also play a similar role. The only difference is that calling the notifyall () method will use all threads blocked by the WAIT () method of the object to the object. All the obstruction is lifted. Of course, only the thread that gets the lock can enter the executable state.
Related Wait and Notify use DEMO:
/** * <pre> * Sub -thread cycle 10 times, then the main thread cycle 100 times, and then return to the sub -thread cycle 10 times, * then return to the main thread cycle 100 times, so 50 times * </presle > * @AUTHOR KETQI */ Public Class WaitNotifyDemo {Public Static Void Main (String [] ARGS) {Final Business Business = New Business (); New Thread (NE w runnable () {@Override Public void run () {for (int i = 1; i <= 50; i ++) {businesSs.sub (i);}}}). Start (); for (int i = 1; i <= 50; i ++) {business.min (i); }}} Class Business {Private Boolean IsmainthRead = TRUE; Public Synchronized void Sub (int i) {While (! IsmainthRead) {try {this.wait ();} Catch (InterruptedException E) {e.printstacktrace ();}} for (int j = 1; j <= 10; j ++) {system.out.println ("sub -thread sequence of" + j + ", loop of" + i);} IsmainThread = false; this.notify (); } Portlic Synchronized Void Main (int i) {While (ismainthread) {Try {this.wait ();} Catch (InterruptedException E) {e.printstacktrace ();} Fors (Int j = 1; j <= 100; j ++) {System.out.println ("Main Thread Sequence of" + J + ", loop of" + i);} IsmainthRead = TRUE; this.notify ();}}The above is all the contents of this article. I hope everyone can like it.