生产者生产数据到缓冲区中,消费者从缓冲区中取数据。

如果缓冲区已经满了,则生产者线程阻塞;

如果缓冲区为空,那么消费者线程阻塞。

2、方式一:synchronized、wait和notify               

  1 package producerConsumer;
  2 //wait 和 notify
  3 public class ProducerConsumerWithWaitNofity {
  4     public static void main(String[] args) {
  5         Resource resource = new Resource();
  6         //生产者线程
  7         ProducerThread p1 = new ProducerThread(resource);
  8         ProducerThread p2 = new ProducerThread(resource);
  9         ProducerThread p3 = new ProducerThread(resource);
 10         //消费者线程
 11         ConsumerThread c1 = new ConsumerThread(resource);
 12         //ConsumerThread c2 = new ConsumerThread(resource);
 13         //ConsumerThread c3 = new ConsumerThread(resource);
 14     
 15         p1.start();
 16         p2.start();
 17         p3.start();
 18         c1.start();
 19         //c2.start();
 20         //c3.start();
 21     }
 22     
 23     
 24     
 25 }
 26 /**
 27  * 公共资源类
 28  * @author 
 29  *
 30  */
 31 class Resource{//重要
 32     //当前资源数量
 33     private int num = 0;
 34     //资源池中允许存放的资源数目
 35     private int size = 10;
 36 
 37     /**
 38      * 从资源池中取走资源
 39      */
 40     public synchronized void remove(){
 41         if(num > 0){
 42             num--;
 43             System.out.println("消费者" + Thread.currentThread().getName() +
 44                     "消耗一件资源," + "当前线程池有" + num + "个");
 45             notifyAll();//通知生产者生产资源
 46         }else{
 47             try {
 48                 //如果没有资源,则消费者进入等待状态
 49                 wait();
 50                 System.out.println("消费者" + Thread.currentThread().getName() + "线程进入等待状态");
 51             } catch (InterruptedException e) {
 52                 e.printStackTrace();
 53             }
 54         }
 55     }
 56     /**
 57      * 向资源池中添加资源
 58      */
 59     public synchronized void add(){
 60         if(num < size){
 61             num++;
 62             System.out.println(Thread.currentThread().getName() + "生产一件资源,当前资源池有" 
 63             + num + "个");
 64             //通知等待的消费者
 65             notifyAll();
 66         }else{
 67             //如果当前资源池中有10件资源
 68             try{
 69                 wait();//生产者进入等待状态,并释放锁
 70                 System.out.println(Thread.currentThread().getName()+"线程进入等待");
 71             }catch(InterruptedException e){
 72                 e.printStackTrace();
 73             }
 74         }
 75     }
 76 }
 77 /**
 78  * 消费者线程
 79  */
 80 class ConsumerThread extends Thread{
 81     private Resource resource;
 82     public ConsumerThread(Resource resource){
 83         this.resource = resource;
 84     }
 85     @Override
 86     public void run() {
 87         while(true){
 88             try {
 89                 Thread.sleep(1000);
 90             } catch (InterruptedException e) {
 91                 e.printStackTrace();
 92             }
 93             resource.remove();
 94         }
 95     }
 96 }
 97 /**
 98  * 生产者线程
 99  */
100 class ProducerThread extends Thread{
101     private Resource resource;
102     public ProducerThread(Resource resource){
103         this.resource = resource;
104     }
105     @Override
106     public void run() {
107         //不断地生产资源
108         while(true){
109             try {
110                 Thread.sleep(1000);
111             } catch (InterruptedException e) {
112                 e.printStackTrace();
113             }
114             resource.add();
115         }
116     }
117     
118 }
View Code

相关文章: