Examples of producer and consumer modes are used to explain the basic Java class methods such as wait, notify, notifyAll, and yypolicyall.
Wait (), Policy (), and policyall () are Java-based java. lang. Object methods.
General Explanation
Wait (): wait for other threads to wake up in the current thread.
Y (): Wake up a thread waiting for the monitor of this object.
Yyall (): Wake up all threads waiting on this object monitor.
These three methods are the underlying mechanism provided by Java to implement Blocking between threads and to control inter-process communication.
The following example shows how to use these three methods.
/*** 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 Good = new Good ("product-ID" + (++ goodNumber); System. out. println (name + "product:" + good. getName (); channel. put (good );}}}
/*** Item * 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 Queue list <> (); 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); // policyall (); Policy ();}}
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 1 (running y ):
Every time a commodity is produced and called notify, only one consumer is awakened for consumption. The wake-up principle starts from the longest wait time.
Consumer 1 enters the waiting state
Consumer 2 enters the waiting state
Consumer 3 enters the waiting state
Producer 1: product-Id 1
Consumer 1: product-Id 1
Consumer 1 enters the waiting state
Producer 2: product-no. 2
Consumer 2: product-no. 2
Consumer 2 enters the waiting state
Producer 2: product-no. 3
Consumer 3: product-no. 3
Consumer 3 enters the waiting state
Producer 1: product-ID 4
Consumer 1: product-ID 4
Consumer 1 enters the waiting state
Producer 1: product-No. 5
Consumer 2: product-Number 5
Consumer 2 enters the waiting state
...
Run Result Analysis 2 (change policy to policyall ):
Every call to yyall during the production of a product will wake up all consumers.
Consumer 1 enters the waiting state
Consumer 3 enters the waiting state
Consumer 2 enters the waiting state
Producer 2: product-no. 1
Consumer 3 enters the waiting state
Consumer 2: product-Id 1
Consumer 1 enters the waiting state
Consumer 2 enters the waiting state
Producer 2: product-no. 2
Consumer 2: product-no. 2
Consumer 1 enters the waiting state
Consumer 3 enters the waiting state
Consumer 2 enters the waiting state
Producer 2: product-no. 3
Consumer 2: product-no. 3
...
Source: http://www.taoweiji.cn/index.php/archives/112