生产/消费模型的java实现

转自:http://ronaldoly.iteye.com/blog/1775333

生产者消费者模型,其实就是一个(生产者)负责产生数据,另一个(消费者)负责使用数据,这里的数据就是生产者和消费者共用的资源,为了让生产者和消费者能有序地使用公共资源,需要引入锁(synchronized)的概念----在一段代码中,将那一段需要很多线程共同使用的代码(相当于共享资源)用synchronized关键字定义,相当于给这一段区域进行了加锁,当有线程来操作它时,就会对其加锁,其他的线程在此时若准备操作它只能排队等待,等第一个线程操作完成,锁解除之后,才能操作。

      下面实现的生产消费模型主要是:

       1.仓库中无产品时,生产者开始生产一件放入仓库,通知消费者来取;

       2.消费者从仓库中取出产品后,仓库没有库存,通知生产者去继续生产。

       3.生产者和消费者是两个互不干扰的线程,但是又有一定的联系,联系就是通过仓库这个被锁定的区域实现的。

      4.wait()和notify()方法就是生产者和消费者线程之间的联系,当一方在使用公共资源时,另一方的状态为wait,当这一方使用公共资源完毕后,会notify(通知)等待的一方。

 

生产者代码:

Java代码   收藏代码
  1. package Producer_customer0123;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class ProducerThread extends Thread {  
  6.   
  7.     private List<Things> thingslist;//产品队列  
  8.     int count=0;  
  9.       
  10.     public ProducerThread(List<Things> thingslist){  
  11.         this.thingslist=thingslist;  
  12.     }  
  13.     public void run(){  
  14.         while(true){  
  15.             //休眠2秒  
  16.             try {  
  17.                 Thread.sleep(2000);  
  18.             } catch (InterruptedException e1) {  
  19.                   
  20.                 e1.printStackTrace();  
  21.             }  
  22.               
  23.             synchronized (thingslist) {  
  24.                 while(thingslist.size()>0){//如果有产品,则等待  
  25.                     try {  
  26.                         thingslist.wait();  
  27.                           
  28.                         System.out.println("生产者在等待-------生产者线程");  
  29.                           
  30.                     } catch (InterruptedException e) {  
  31.                         e.printStackTrace();  
  32.                     }  
  33.                 }  
  34.                 while(thingslist.size()==0){//如果没有产品,则生产产品,并且通知消费者  
  35.                     Things newthing=new Things();  
  36.                     count++;  
  37.                     newthing.id=count;  
  38.                     newthing.name="第"+count+"个产品";  
  39.                     thingslist.add(newthing);//加入到队列中  
  40.                       
  41.                     thingslist.notify();//通知消费者  
  42.                     System.out.println("生产者生产了"+count+"号产品-------生产者线程");  
  43.                 }  
  44.             }  
  45.         }  
  46.     }  
  47. }  

 

消费者代码:

Java代码   收藏代码
  1. package Producer_customer0123;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class CustomerThread extends Thread{  
  6.   
  7.     private List<Things> thingslist;  
  8.   
  9.     public CustomerThread(List<Things> thingslist) {  
  10.         this.thingslist=thingslist;  
  11.     }  
  12.   
  13.     public void run(){  
  14.           
  15.         while(true){  
  16.               
  17.             synchronized (thingslist) {  
  18.                 while(thingslist.size()>0){//如果队列中有产品,消费者就取出来  
  19.                     for(int i=0;i<thingslist.size();i++){  
  20.                           
  21.                         thingslist.remove(i);  
  22.                         thingslist.notify();  
  23.                         System.out.println("消费者取出了队列中第"+i+"号产品----消费者线程");  
  24.                     }  
  25.                 }  
  26.                 while(thingslist.size()==0){  
  27.                     try {  
  28.                         System.out.println("队列中已没有产品了----消费者线程");  
  29.                         thingslist.wait();  
  30.                           
  31.                     } catch (InterruptedException e) {  
  32.                         // TODO Auto-generated catch block  
  33.                         e.printStackTrace();  
  34.                     }  
  35.                 }  
  36.             }  
  37.         }  
  38.     }  
  39.   
  40. }  

 

主函数:

Java代码   收藏代码
  1. package Producer_customer0123;  
  2.   
  3. import java.util.LinkedList;  
  4. import java.util.List;  
  5.   
  6. public class Test {  
  7.   
  8.       
  9.     public static void main(String args[]){  
  10.         //创建队列  
  11.         List<Things> thingslist=new LinkedList<Things>() ;  
  12.           
  13.         //启动线程  
  14.         ProducerThread producer=new ProducerThread(thingslist);  
  15.         CustomerThread customer=new CustomerThread(thingslist);  
  16.         producer.start();  
  17.         customer.start();  
  18.     }  
  19.   
  20. }  

 

运行结果如下:

 

 

 

      结论:通过打印会发现生产者线程和消费者线程会按照某一个顺序依次对thingslist进行操作,对比两段线程的代码,会发现,一个很大的不同就是wait()和notify()的时机,生产者和消费者总是相反的,这也正好解释了在同一时刻,thingslist只允许一个线程对他进行操作。

     以上就是我对生产消费模型的一些认识。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值