java生产消费线程小例子

生产消费线程型是理解多线程的一个好例子,实际上,准确说应该是“生产者-消费者-仓储”模型,离开了仓储,生产者消费者模型就显得没有说服力了。

  对于此模型,应该明确一下几点:

  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() 方法,起到的是一个通知作用,不释放锁,也不获取锁。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值