Wait (), notify (), and Notifyall () are all methods of the Java base class Java.lang.Object.
Popular explanations
Wait (): Waits for another thread to wake up on the current thread.
Notify (): Wakes up a thread that is waiting for this object's monitor.
Notifyall (): Wakes up all the threads waiting on this object monitor.
These three methods are the underlying mechanism provided by the Java language to implement inter-thread blocking (Blocking) and control in-process scheduling (inter-process communication).
The use of these three methods is explained below through a producer/consumer example.
/** * consumer * Created by Wiki on 16/1/28. */public class Customer implements Runnable {private String Name Private channel channel; Public Customer (String name, channel Channel) {this.name = name; This.channel = Channel; @Override public void Run () {while (true) {Good good = Channel.get (); if (good! = null) {SYSTEM.OUT.PRINTLN (name + "Get product:" + good.getname ()); } else {synchronized (channel) {try {System.out.println (name + "Enter Wait"); Channel.wait (); } catch (Interruptedexception e) {e.printstacktrace (); } } } } }}
/** * producer * Created by Wiki on 16/1/28. */public class Producer implements Runnable {private static volatile int goodnumber = 0; private String name; Private channel channel; Public Producer (String name, channel Channel) {this.name = name; This.channel = Channel; @Override public void Run () {while (true) {int sleep = new Random (). Nextint (2000); try {thread.sleep (sleep); } catch (Interruptedexception e) {e.printstacktrace (); } Good good = new Good ("Item-Number" + (++goodnumber)); SYSTEM.OUT.PRINTLN (name + "Production goods:" + good.getname ()); Channel.put (good); } }}
/** * Goods * Created by Wiki on 16/1/28. */public class Good { private String name; Public good (String name) { this.name = name; } Public String GetName () { return name; } public void SetName (String name) { this.name = name; }}
/** * Consumption channel * Created by Wiki on 16/1/28. */public class Channel { private queue<good> goodlist = new linkedlist<> (); Public synchronized Good Get () { if (goodlist.size () = = 0) { return null; } Good good = Goodlist.remove (); return good; } Public synchronized void put (good good) { goodlist.add (good);// Notifyall (); Notify (); }}
public class Main {public static void Main (string[] args) { channel channel = new Channel (); New Thread (New Producer ("Producer 1", channel)). Start (); New Thread (New Producer ("Producer 2", channel)). Start (); New Thread (New Producer ("Producer 2", channel)). Start (); New Thread (New Customer ("Consumer 1", channel)). Start (); New Thread (New Customer ("Consumer 2", channel)). Start (); New Thread (New Customer ("Consumer 3", channel)). Start (); }}
Run result analysis One (notify):
Each time a product is called notify, only one consumer is awakened to consume, and the awakening principle is the longest waiting time to start.
Consumer 1 into wait
Consumer 2 into wait
Consumer 3 into wait
Producer 1 Production commodities: Goods-No. 1
Consumer 1 Get Product: Item-No. 1
Consumer 1 into wait
Producer 2 Production commodities: Goods-No. 2
Consumer 2 Get Product: Item-No. 2
Consumer 2 into wait
Producer 2 Production commodities: Goods-No. 3
Consumer 3 Get Product: Item-No. 3
Consumer 3 into wait
Producer 1 Production commodities: Goods-No. 4
Consumer 1 Get Product: Item-No. 4
Consumer 1 into wait
Producer 1 Production commodities: Goods-No. 5
Consumer 2 Get Product: Item-No. 5
Consumer 2 into wait
...
Run Results Analysis II (change notify to Notifyall):
Every product that is produced is called Notifyall, and all consumers are awakened.
Consumer 1 into wait
Consumer 3 into wait
Consumer 2 into wait
Producer 2 Production commodities: Goods-No. 1
Consumer 3 into wait
Consumer 2 Get Product: Item-No. 1
Consumer 1 into wait
Consumer 2 into wait
Producer 2 Production commodities: Goods-No. 2
Consumer 2 Get Product: Item-No. 2
Consumer 1 into wait
Consumer 3 into wait
Consumer 2 into wait
Producer 2 Production commodities: Goods-No. 3
Consumer 2 Get Product: Item-No. 3
...
Source: http://www.taoweiji.cn/index.php/archives/112
Explaining Java base class methods through the producer consumer model Wait, notify, Notifyall