Condition and wait and Policy, conditionpolicy
Ondition can implement multi-channel Condition, and notify y can only notify a random
For example, ABC processes one thing separately. The rule is that A processes notification B, B processes notification C, and C notifies A. If notrfy is used, therefore, we can use Condition to create three Condition objects in ABC to specify the await object and the single object;
For example, put eggs, take eggs, and eat eggs first.
Public class EggServlet {private Lock lock = new ReentrantLock (); Condition put = lock. newCondition (); Condition get = lock. newCondition (); Condition eatlock = lock. newCondition (); int num = 0; int eat = 0; public void putEgg () {lock. lock (); try {while (num! = 0) {try {put. await ();} catch (InterruptedException e) {e. printStackTrace () ;}} num ++; System. out. println ("put eggs" + num); get. signal ();} catch (Exception ep) {System. out. println (ep. toString ();} finally {lock. unlock () ;}} public void getEgg () {lock. lock (); while (num = 0) {try {get. await ();} catch (InterruptedException e) {e. printStackTrace () ;}} System. out. println ("get eggs" + num); num --; eat ++; eatlock. signal (); lock. unlock ();} public void eatEgg () {lock. lock (); while (eat = 0) {try {eatlock. await ();} catch (InterruptedException e) {e. printStackTrace () ;}} System. out. println ("eat eggs" + num); System. out. println ("============================"); eat --; put. signal (); lock. unlock () ;}} public class TestClient {public static void main (String [] args) {EggServlet egg = new EggServlet (); <span style = "white-space: pre "> </span> Thread t1 = new Thread (new PutThead (egg); <span style =" white-space: pre "> </span> Thread t2 = new Thread (new GetThead (egg); <span style =" white-space: pre "> </span> Thread t3 = new Thread (new EatThead (egg); <span style =" white-space: pre "> </span> <span style =" white-space: pre "> </span>/* t1.start (); <span style =" white-space: pre "> </span> t2.start (); <span style =" white-space: pre "> </span> t3.start (); */<span style = "white-space: pre"> </span> <span style = "white-space: pre"> </span> ExecutorService pool = Executors. newCachedThreadPool (); <span style = "white-space: pre"> </span> pool.exe cute (t1); <span style = "white-space: pre "> </span> pool.exe cute (t2); <span style =" white-space: pre "> </span> pool.exe cute (t3 );}} class PutThead implements Runnable {private EggServlet egg; public PutThead (EggServlet egg) {this. egg = egg ;}@ Overridepublic void run () {for (int I = 0; I <10; I ++) {egg. putEgg () ;}} class GetThead implements Runnable {private EggServlet egg; public GetThead (EggServlet egg) {this. egg = egg ;}@ Overridepublic void run () {for (int I = 0; I <10; I ++) {egg. getEgg () ;}} class EatThead implements Runnable {private EggServlet egg; public EatThead (EggServlet egg) {this. egg = egg ;}@ Overridepublic void run () {for (int I = 0; I <10; I ++) {egg. eatEgg ();}}}
Put it into eggs 1 get the eggs 1 eat the eggs 0 ========================== put it into the eggs 1 get the eggs 1 eat the eggs 0 = = ===== put in the eggs 1 get the eggs 1 eat eggs 0 ==================================== put in the eggs 1 get go to eggs 1, eat eggs 0 ============================ put the eggs 1, get the eggs 1, eat the eggs 0 ==== = ========== put in the eggs 1 get the eggs 1 eat the eggs 0 ============================ put I got egg 1 and got it.