java 中多線程之間的通訊之生產者和消費者 (多個線程之間的通訊)

來源:互聯網
上載者:User

標籤:

在真實開發 中關於多線程的通訊的問題用到下邊的例子是比較多的

不同的地方時if 和while 的區別 如果只是兩個線程之間的通訊,使用if是沒有問題的。

但是在多個線程之間就會有問題

  1 /*  2  * 這個例子用來解釋多個生產者和多個消費者的情況  3  */  4   5 /*  6  * 資產庫  7  */  8 class Resource   9 { 10     private String name; 11     private int count = 1; 12     private boolean flag = false; 13     public synchronized void set (String name){ 14          15         while (flag) 16             try {this.wait();} 17             catch(Exception e){ 18                  19             } 20             this .name = name + "--" + count++; 21             System.out.println(Thread.currentThread().getName()+"生產商品"+this.name); 22             flag = true; 23             this.notifyAll(); 24              25         } 26      27      28     public synchronized void out(){ 29         while (!flag) 30             try {this.wait();} 31             catch(Exception e){ 32                  33             } 34             System.out.println(Thread.currentThread().getName() + "消費了" + this.name); 35             flag = false; 36             this.notifyAll(); 37         } 38          39     } 40  41 /* 42  * 生產者 43  */ 44 class Producer implements Runnable 45 { 46     private Resource res; 47     Producer (Resource res){ 48          49         this.res = res; 50     } 51     public void run(){ 52          53         while (true){ 54              55             res.set ("牙膏"); 56         } 57     } 58      59      60 } 61 /* 62  * 消費者 63  */ 64 class Consumer implements Runnable 65 { 66  67     private Resource res; 68     Consumer(Resource res){ 69          70         this.res = res; 71     } 72     public void run(){ 73          74         while (true){ 75             res.out(); 76         } 77     } 78 } 79 public class producerConsumerDemo { 80  81     public static void main(String[] args) { 82         // TODO Auto-generated method stub 83         Resource res = new Resource(); 84          85         Producer p1 = new Producer(res); 86         Producer p2 = new Producer(res); 87          88         Consumer c1 = new Consumer(res); 89         Consumer c2 = new Consumer(res); 90          91         Thread t1 = new Thread(p1); 92         Thread t2 = new Thread(p2); 93         Thread t3 = new Thread(c1); 94         Thread t4 = new Thread(c2); 95          96         t1.start(); 97         t2.start(); 98         t3.start(); 99         t4.start();100 101 102 103     }104 105 }

 

java 中多線程之間的通訊之生產者和消費者 (多個線程之間的通訊)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.