java线程:三种方式实现生产者消费者问题_1

本文详细介绍了使用Java语言实现生产者消费者问题的三种方式,包括消费者、生产者和仓库类的代码实现及测试结果,展示了同步控制和线程协作的基本原理。


java线程:三种方式实现生产者消费者问题_1

分类: java_线程  45人阅读 评论(0) 收藏 举报


本人采用java语言,用三种方式实现生产者——消费者的经典问题,至于生产者和消费的问题就不多说了,以下是第一种方式的具体代码:

[java] view plaincopy
  1. <span style="font-family:Arial;">  
  2. /** 
  3.  * 消费者:生产者:生产者和消费者共用一个仓库 
  4.  */  
  5. public class Consumer implements Runnable {  
  6.   
  7.     private Storage storage;  
  8.   
  9.     public Consumer(Storage storage) {  
  10.         this.storage = storage;  
  11.     }  
  12.       
  13.     // 消费  
  14.     public void cunsume() {  
  15.         while (true) {  
  16.             storage.get();  
  17.               
  18.             try {  
  19.                 Thread.sleep(1500); // 每消费完一件产品,休息1.5秒  
  20.             } catch (InterruptedException e) {  
  21.                 throw new RuntimeException(e);  
  22.             }  
  23.         }  
  24.     }  
  25.       
  26.     @Override  
  27.     public void run() {  
  28.         this.cunsume();  
  29.     }  
  30. }  
  31. </span>  



[java] view plaincopy
  1. <span style="font-family:Arial;">  
  2. /** 
  3.  * 生产者:生产者和消费者共用一个仓库 
  4.  */  
  5. public class Producer implements Runnable {  
  6.       
  7.     private Storage storage;  
  8.       
  9.     public Producer(Storage storage){  
  10.         this.storage = storage;  
  11.     }  
  12.       
  13.     @Override  
  14.     public void run(){  
  15.         this.produce();  
  16.     }  
  17.       
  18.     // 生产  
  19.     public void produce(){  
  20.         while(true){  
  21.             storage.put();  
  22.               
  23.             try {  
  24.                 Thread.sleep(1000); // 每生产完一件产品,休息1秒  
  25.             } catch (InterruptedException e) {  
  26.                 throw new RuntimeException(e);  
  27.             }  
  28.         }  
  29.     }  
  30. }  
  31. </span>  

[java] view plaincopy
  1. <span style="font-family:Arial;">  
  2. /** 
  3.  * 仓库: 
  4.  * 先生产的先消费:使用队列的方式实现 
  5.  */  
  6. public class Storage {  
  7.       
  8.     public static final int MAX_SIZE = 100// 仓库的最大货存  
  9.       
  10.     // 容器,为了使用方便,没有面向接口编程,使用LinkedList作为实现类  
  11.     private LinkedList<Object> container = new LinkedList<Object>();   
  12.       
  13.     // 从仓库中存放一个产品:  
  14.     public synchronized void put(){  
  15.         try {  
  16.             while (container.size() == MAX_SIZE) {  
  17.                 try {  
  18.                     this.wait();  
  19.                 } catch (InterruptedException e) {  
  20.                     throw new RuntimeException(e);  
  21.                 }  
  22.             }  
  23.               
  24.             System.out.println("put()等待结束,仓库现有数量为"+ container.size() +", 开始生产");  
  25.             container.addLast(new Object());   
  26.               
  27.         } finally {  
  28.             this.notifyAll(); // 通知其他线程  
  29.         }  
  30.     }  
  31.       
  32.     // 从仓库中取出一个产品:  
  33.     public synchronized Object get(){  
  34.         try {  
  35.             while (container.size() == 0) {  
  36.                 try {  
  37.                     this.wait();  
  38.                 } catch (InterruptedException e) {  
  39.                     throw new RuntimeException(e);  
  40.                 }  
  41.             }  
  42.               
  43.             System.out.println("get()等待结束,仓库现有数量为"+ container.size() +", 开始取出");  
  44.             return container.removeFirst();// 取出  
  45.         } finally {  
  46.             this.notifyAll(); // 通知其他线程  
  47.         }  
  48.     }  
  49. }</span>  


以下是测试代码:

[java] view plaincopy
  1. <span style="font-family:Arial;">  
  2. public class Test {  
  3.       
  4.     public static void main(String[] args) {  
  5.         Storage storage = new Storage();  
  6.           
  7.         // 生产者和消费者两个线程类,用的必须是同一个仓库,即同一个仓库对象storage  
  8.         Consumer con = new Consumer(storage);  
  9.         Producer pro = new Producer(storage);  
  10.           
  11.         new Thread(pro).start();  
  12.         new Thread(pro).start();  
  13.         new Thread(pro).start();  
  14.         new Thread(pro).start();  
  15.           
  16.           
  17. //      new Thread(con).start();  
  18. //      new Thread(con).start();  
  19. //      new Thread(con).start();  
  20. //      new Thread(con).start();  
  21.         new Thread(con).start();  
  22.     }  
  23. }</span>  

以下是测试代码结果:

get()等待结束,仓库现有数量为100, 开始取出
put()等待结束,仓库现有数量为99, 开始生产
get()等待结束,仓库现有数量为100, 开始取出
put()等待结束,仓库现有数量为99, 开始生产
get()等待结束,仓库现有数量为100, 开始取出
put()等待结束,仓库现有数量为99, 开始生产
get()等待结束,仓库现有数量为100, 开始取出
put()等待结束,仓库现有数量为99, 开始生产
get()等待结束,仓库现有数量为100, 开始取出
put()等待结束,仓库现有数量为99, 开始生产
get()等待结束,仓库现有数量为100, 开始取出
put()等待结束,仓库现有数量为99, 开始生产
get()等待结束,仓库现有数量为100, 开始取出
put()等待结束,仓库现有数量为99, 开始生产

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值