Wait,notify,notifyall is an instance method defined in the object class, used to control the state of threads, and when a thread is collaborating, everyone uses the Notify () or Notifyall () method. Where wait and notify is an important part of the Java synchronization mechanism, which needs to be combined with the Synchronized keyword to use, when calling an object's wait and Notify/notifyall, The calling code must be guaranteed to be synchronous with the object, meaning it must be equal to synchronized (object) {...} To call obj's wait and Notify/notifyall three methods, or you will get an error: Java.lang.IllegalMonitorStateException:current Thread not owner ( This means that because there is no synchronization, the state of the thread lock on the object is indeterminate and cannot be called.
The purpose of wait is to expose the object lock, so it is necessary to ensure that the lock.wait () method is called in the Sync Code of lock, so that other threads can wake up the thread waiting for the object in the pool by notify the object. The same notify also releases the object lock, which must obtain the lock of the object before it is called, otherwise it will also report an exception. Therefore, the thread automatically releases its possession of the object lock, will not go to apply for the object lock, only when the threads are awakened or to reach the maximum sleep time, it is again the right to win the object lock
Main methods:
(1). Wait ()
Wait for the object's synchronization lock, need to obtain the object's synchronization lock to call this method, otherwise the compilation can pass, but the runtime will receive an exception: Illegalmonitorstateexception. Calling the Wait () method of an arbitrary object causes the thread to block, the thread cannot continue execution, and the lock on the object is freed.
(2). Notify ()
Wake up the thread waiting for the object to synchronize the lock (wake only one, if there are multiple waits), note that when this method is called, it does not wake up the thread of a waiting state exactly, but is determined by the JVM to wake up which thread, and not by priority. Calling the Notify () method of an arbitrary object causes a randomly selected unblocking in a thread that is blocked by calling the wait () method of the object (but is not really executable until the lock is acquired).
(3). Notifyall ()
Wake up all waiting threads, notice that the thread that wakes up is notify before wait, and has no effect on the wait thread after notify.
Look at the actual effect with an example, turn on two lines and a thread to print 1 to 52 numbers, one print A to Z letters, ask, print two numbers, print a letter, and then print in alternating order, the code is as follows:
/*** Create by spy on 2018/6/4*/ Public classShuzizimuthread { Public Static voidMain (string[] args) {Object Object=NewObject (); Shuzi Shuzi=NewShuzi (object); Zimu Zimu=NewZimu (object); Thread T=NewThread (Shuzi); T.setname ("Shuzi"); Thread T1=NewThread (Zimu); T1.setname ("Zimu"); T.start ();//Digital Threads Run firstT1.start (); }}classShuziImplementsrunnable{PrivateObject object; //declaring a reference to a class PublicShuzi (Object object) { This. Object =object; } Public voidrun () {synchronized(object) {//locked for(inti=1;i<53;i++) {System.out.print (i+","); if(i%2==0) {object.notifyall ();//Wake up other threads competing for permissions Try{object.wait ();//Release the lock and enter the waitSYSTEM.OUT.PRINTLN ("Digital print class hits full print the current object has an object lock thread" +thread.currentthread (). GetName ());//output The name of the thread that currently owns the lock}Catch(interruptedexception e) {e.printstacktrace (); } } } } }}classZimuImplementsrunnable{PrivateObject object; PublicZimu (Object object) { This. Object =object; } Public voidrun () {synchronized(object) { for(intj=65;j<91;j++){ Charc = (Char) J; System.out.print (c); Object.notifyall ();//Wake up other threads competing for permissions Try{object.wait ();//Release the lock and enter the waitSystem.out.println ("Letter printing class hits full print the current object has an object lock thread" +thread.currentthread (). GetName ());//output The name of the thread that currently owns the lock}Catch(interruptedexception e) {e.printstacktrace (); } } } }}
Results of the actual operation:
1,2,a Digital Print class prints the thread that the current object has an object lock on Shuzi
3,4, the letter print class prints the thread that the current object has an object lock on Zimu
b Digital Print class print the current object has an object lock thread Shuzi
5,6, the letter print class prints the thread that the current object has an object lock on Zimu
C Digital Print class print the current object has an object lock thread Shuzi
7,8, the letter print class prints the thread that the current object has an object lock on Zimu
D Digital Print class print the current object has an object lock thread Shuzi
9,10, the letter print class prints the thread that the current object has an object lock on Zimu
e Digital Print class print the current object has an object lock thread Shuzi
11,12, the letter print class prints the thread that the current object has an object lock on Zimu
F Digital Print class Print the current object has an object lock thread Shuzi
13,14, the letter print class prints the thread that the current object has an object lock on Zimu
G Digital Print class print the current object has an object lock thread Shuzi
15,16, the letter print class prints the thread that the current object has an object lock on Zimu
H Digital Print class print the current object has an object lock thread Shuzi
17,18, the letter print class prints the thread that the current object has an object lock on Zimu
I digital print class print the current object has an object lock thread Shuzi
19,20, the letter print class prints the thread that the current object has an object lock on Zimu
J Digital Print class print the current object has an object lock thread Shuzi
21,22, the letter print class prints the thread that the current object has an object lock on Zimu
K Digital Print class print the current object has an object lock thread Shuzi
23,24, the letter print class prints the thread that the current object has an object lock on Zimu
L Digital Print class print the current object has an object lock thread Shuzi
25,26, the letter print class prints the thread that the current object has an object lock on Zimu
M digital Print class print the current object has an object lock thread Shuzi
27,28, the letter print class prints the thread that the current object has an object lock on Zimu
n Digital Print class print the current object has an object lock thread Shuzi
29,30, the letter print class prints the thread that the current object has an object lock on Zimu
o Digital Print class Print the current object has an object lock thread Shuzi
31,32, the letter print class prints the thread that the current object has an object lock on Zimu
P Digital Print class print the current object has an object lock thread Shuzi
33,34, the letter print class prints the thread that the current object has an object lock on Zimu
Q Digital Print class print the current object has an object lock thread Shuzi
35,36, the letter print class prints the thread that the current object has an object lock on Zimu
R Digital Print class print the current object has an object lock thread Shuzi
37,38, the letter print class prints the thread that the current object has an object lock on Zimu
The S-Digital print class prints the thread Shuzi the current object has an object lock
39,40, the letter print class prints the thread that the current object has an object lock on Zimu
The T-print class prints the thread Shuzi the current object has an object lock
41,42, the letter print class prints the thread that the current object has an object lock on Zimu
U digital Print class Print the current object has an object lock thread Shuzi
43,44, the letter print class prints the thread that the current object has an object lock on Zimu
V-Print class print the current object has an object lock thread Shuzi
45,46, the letter print class prints the thread that the current object has an object lock on Zimu
W Digital Print class Print the current object has an object lock thread Shuzi
47,48, the letter print class prints the thread that the current object has an object lock on Zimu
The X-print class prints the thread Shuzi the current object has an object lock
49,50, the letter print class prints the thread that the current object has an object lock on Zimu
The Y-print class prints the thread Shuzi the current object has an object lock
51,52, the letter print class prints the thread that the current object has an object lock on Zimu
Z-Print class print the current object has an object lock thread Shuzi
Results Analysis:
As you can see from the results:
After the letter is typed in a print class, the call ends
Object.notifyall ();//Wake up other threads competing for permissions
Object.wait ();//Release lock, enter wait
, the thread that owns the object lock is Shuzi
In the digital printing class, complete the call.
Object.notifyall ();//Wake up other threads competing for permissions
Object.wait ();//Release lock, enter wait
, the thread that owns the object lock is Zimu
Understanding of Wait,notify,notifyall in Java object objects