Condition and wait and notify

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.