Ondition can implement multiple Condition,notify can only notify a random
Such as:ABCthree threads to deal with one thing, the rule isAfinish processing the notificationB,Bfinish processing the notificationC,cNoticeA; If you useNotrfycan't practice this, so we can useConditioncome toNewoutABCthree xConditionobject to specify which object is specifiedawaitwhich object Single;
such as: first put eggs, take eggs, eat eggs
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 the 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--;p ut.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 PutT Head (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.execute (t1); <span style = "White-space:pre" ></span>pool.execute (T2); <span style= "White-space:pre" ></span> Pool.execute (T3);}} Class Putthead implements Runnable {private Eggservlet egg;public putthead (Eggservlet egg) {This.egg= Egg;} @Overridepublic void Run () {for (int i = 0; i <; 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 <; 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 <; i++) {Egg.eategg ();}}}
Put eggs 1 get eggs 1 eat eggs 0===================== put eggs 1 got eggs 1 eat eggs 0===================== put eggs 1 got eggs 1 eat eggs 0==================== = put eggs 1 get eggs 1 eat eggs 0===================== put eggs 1 got eggs 1 eat eggs 0===================== put eggs 1 got eggs 1 eat eggs 0=================== = = put eggs 1 get eggs 1 eat eggs 0===================== put eggs 1 got eggs 1 eat eggs 0===================== put eggs 1 got eggs 1 eat eggs 0================== = = = put eggs 1 get the eggs 1 eat eggs 0=====================
Condition and wait and notify