Condition and wait and Policy, conditionpolicy

Source: Internet
Author: User

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.


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.