java生產消費線程小例子

來源:互聯網
上載者:User

生產消費線程型是理解多線程的一個好例子,實際上,準確說應該是“生產者-消費者-倉儲”模型,離開了倉儲,生產者消費者模型就顯得沒有說服力了。

  對於此模型,應該明確一下幾點:

  1、生產者僅僅在倉儲未滿時候生產,倉滿則停止生產。

  2、消費者僅僅在倉儲有產品時候才能消費,倉空則等待。

  3、當消費者發現倉儲沒產品可消費時候會通知生產者生產。

  4、生產者在生產出可消費產品時候,應該通知等待的消費者去消費。

代碼:

1生產線程類:

Code:
  1. import java.util.List;  
  2.   
  3. public class Product implements Runnable {  
  4.     private List container = null;  
  5.     private int count;  
  6.   
  7.     public Product(List lst) {  
  8.         this.container = lst;  
  9.     }  
  10.   
  11.     public void run() {  
  12.         while (true) {  
  13.             synchronized (container) {  
  14.                 if (container.size() >= MultiThread.MAX) {//如果大於設定的MAX庫存,生產者就wait等待  
  15.                     try {  
  16.                         container.wait();  
  17.                     } catch (InterruptedException e) {  
  18.                         e.printStackTrace();  
  19.                     }  
  20.                 }  
  21.                 try {  
  22.                     Thread.sleep(100);  
  23.                 } catch (InterruptedException e) {  
  24.                     e.printStackTrace();  
  25.                 }  
  26.                 container.add(new Object());  
  27.                 container.notify();//notify叫醒其他在container上的線程(消費者)  
  28.                 System.out.println("我生產了" + (++count) + "個");  
  29.   
  30.             }  
  31.         }  
  32.     }  
  33. }  

--

2消費線程:

Code:
  1. import java.util.List;  
  2.   
  3. public class Consume implements Runnable {  
  4.     private List container = null;  
  5.     private int count;  
  6.   
  7.     public Consume(List lst) {  
  8.         this.container = lst;  
  9.     }  
  10.   
  11.     public void run() {  
  12.         while (true) {  
  13.             synchronized (container) {  
  14.                 if (container.size() == 0) {//如果容器已經空了,消費者wait等待(生產者)  
  15.                     try {  
  16.                         container.wait();// 放棄鎖  
  17.                     } catch (InterruptedException e) {  
  18.                         e.printStackTrace();  
  19.                     }  
  20.                 }  
  21.                 try {  
  22.                     Thread.sleep(100);  
  23.                 } catch (InterruptedException e) {  
  24.                     e.printStackTrace();  
  25.                 }  
  26.                 container.remove(0);  
  27.                 container.notify();//叫醒生產者  
  28.                 System.out.println("我吃了" + (++count) + "個");  
  29.             }  
  30.         }  
  31.     }  
  32. }  

--

3.運行測試:

Code:
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.   
  4. public class MultiThread {  
  5.     private List container = new ArrayList();  
  6.     public final static int MAX = 3;//最大庫存  
  7.   
  8.     public static void main(String args[]) {  
  9.         MultiThread m = new MultiThread();  
  10.         new Thread(new Consume(m.getContainer())).start();  
  11.         new Thread(new Product(m.getContainer())).start();  
  12.     }  
  13.   
  14.     public List getContainer() {  
  15.         return container;  
  16.     }  
  17.   
  18.     public void setContainer(List container) {  
  19.         this.container = container;  
  20.     }  
  21. }  

 本例中當發現不能滿足生產或者消費條件的時候,調用對象的wait方法,wait方法的作用是釋放當前線程的所獲得的鎖,並調用對象的notify() 方法,通知(喚醒)該對象上其他等待線程,使得其繼續執行。這樣,整個生產者、消費者線程得以正確的協作執行。

  notify() 方法,起到的是一個通知作用,不釋放鎖,也不擷取鎖。

聯繫我們

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