Java多线程之~~~~使用wait和notify实现生产者消费者模型

来源:互联网 发布:科技成果登记软件 编辑:程序博客网 时间:2024/05/21 17:04
在多线程开发中,最经典的一个模型就是生产者消费者模型,他们有一个缓冲区,缓冲区有最大限制,当缓冲区满

的时候,生产者是不能将产品放入到缓冲区里面的,当然,当缓冲区是空的时候,消费者也不能从中拿出来产品,这就

涉及到了在多线程中的条件判断,Java为了实现这些功能,提供了wait和notify方法,他们可以在线程不满足要求的时候

让线程让出来资源等待,当有资源的时候再notify他们让他们继续工作,下面我们用实际的代码来展示如何使用wait和

notify来实现生产者消费者这个经典的模型。


首先是缓冲区的实现,我们使用LinkedList来代替


[java] view plain copy print?
  1. package com.bird.concursey.charpet2;  
  2.   
  3. import java.util.Date;  
  4. import java.util.LinkedList;  
  5. import java.util.List;  
  6.   
  7. public class EventStorage {  
  8.       
  9.     private int maxSize;  
  10.       
  11.     private List<Date> storage;  
  12.       
  13.     public EventStorage() {  
  14.         maxSize = 10;  
  15.         storage = new LinkedList<Date>();  
  16.     }  
  17.       
  18.     public synchronized void set() {  
  19.         while(storage.size() == maxSize) {  
  20.             try {  
  21.                 wait();  
  22.             } catch (InterruptedException e) {  
  23.                 e.printStackTrace();  
  24.             }  
  25.         }  
  26.           
  27.         storage.add(new Date());  
  28.         System.out.printf("Set: %d",storage.size());  
  29.         notifyAll();  
  30.     }  
  31.       
  32.     public synchronized void get() {  
  33.         while(storage.size() == 0) {  
  34.             try {  
  35.                 wait();  
  36.             } catch (InterruptedException e) {  
  37.                 // TODO Auto-generated catch block  
  38.                 e.printStackTrace();  
  39.             }  
  40.         }  
  41.           
  42.         System.out.printf("Get: %d: %s",storage.size(),((LinkedList<?>)storage).poll());  
  43.         notifyAll();  
  44.     }  
  45. }  


然后就是生产者和消费者


[java] view plain copy print?
  1. package com.bird.concursey.charpet2;  
  2.   
  3. public class Producer implements Runnable {  
  4.       
  5.     private EventStorage storge;  
  6.       
  7.     public Producer(EventStorage storage) {  
  8.         this.storge = storage;  
  9.     }  
  10.   
  11.     @Override  
  12.     public void run() {  
  13.         for(int i = 0; i < 100; i++) {  
  14.             storge.set();  
  15.         }  
  16.     }  
  17.   
  18. }  



[java] view plain copy print?
  1. package com.bird.concursey.charpet2;  
  2.   
  3. public class Consumer implements Runnable {  
  4.       
  5.     private EventStorage storage;  
  6.       
  7.     public Consumer(EventStorage storage) {  
  8.         this.storage = storage;  
  9.     }  
  10.   
  11.     @Override  
  12.     public void run() {  
  13.         for(int i = 0; i < 100; i++) {  
  14.             storage.get();  
  15.         }  
  16.     }  
  17.       
  18.     public static void main(String[] args) {  
  19.         EventStorage storage = new EventStorage();  
  20.         Producer producer = new Producer(storage);  
  21.         Thread thread1 = new Thread(producer);  
  22.           
  23.         Consumer consumer = new Consumer(storage);  
  24.         Thread thread2 = new Thread(consumer);  
  25.           
  26.         thread2.start();  
  27.         thread1.start();  
  28.     }  
  29. }  

可以看到,这里面就是用了wait和notifyall方法实现了生产者消费方法,具体的运行过程大家可以通过阅读代码来体

会,还是很直观的。

0 0
原创粉丝点击