线程演示生产者和消费者的一个例子

来源:互联网 发布:戴蜜蜡的好处知乎 编辑:程序博客网 时间:2024/05/18 19:19

生产者:向仓库容器container中填充对象,直到达到设置的库存上限MAX,然后就wait等待,并且通知叫醒notify消费者去消费。

生产者代码:

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) {  
  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();  
  28.                 System.out.println("我生产了" + (++count) + "个");  
  29.   
  30.             }  
  31.         }  
  32.     }  
  33. }  

 

消费者:消费仓库container中的货物,当库存为空等于0时就停止等待wait并且通知叫醒notify生产者。

消费者代码:

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) {  
  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. }  

 

运行演示:启动了一个生产者和一个消费者,可以从控制台看到生产和消费的情况。

代码:

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 = 5;  
  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. }  

 

原创粉丝点击