Condition 與wait和notify,conditionnotify

來源:互聯網
上載者:User

Condition 與wait和notify,conditionnotify

ondition可以實現多路Condition,notify只能通知隨機的一個

  如:ABC三個線程分別去處理一件事物,規則是A處理完通知B ,B處理完通知C,C通知A;如果使用notrfy無法做法這一點,所以我們可以使用Condition來new出來ABC三個Condition對象,來特定指明哪個對象await哪個對象single;

如:先放雞蛋、拿雞蛋、吃雞蛋

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("放入了雞蛋" + 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("拿到了雞蛋" + 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("吃雞蛋" + 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.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 < 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();}}}

放入了雞蛋1拿到了雞蛋1吃雞蛋0=====================放入了雞蛋1拿到了雞蛋1吃雞蛋0=====================放入了雞蛋1拿到了雞蛋1吃雞蛋0=====================放入了雞蛋1拿到了雞蛋1吃雞蛋0=====================放入了雞蛋1拿到了雞蛋1吃雞蛋0=====================放入了雞蛋1拿到了雞蛋1吃雞蛋0=====================放入了雞蛋1拿到了雞蛋1吃雞蛋0=====================放入了雞蛋1拿到了雞蛋1吃雞蛋0=====================放入了雞蛋1拿到了雞蛋1吃雞蛋0=====================放入了雞蛋1拿到了雞蛋1吃雞蛋0=====================


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.