当前位置:编程学习 > JAVA >>

生产者消费者模式详解及代码实现

生产者消费者模式说明:
1.生产者只在仓库未满时进行生产,仓库满时生产者进程被阻塞;
2.消费者只在仓库非空时进行消费,仓库为空时消费者进程被阻塞;
3.当消费者发现仓库为空时会通知生产者生产;
4.当生产者发现仓库满时会通知消费者消费;
实现的关键:
共享内存中的两个同步方法,及同步方法中wait()方法的调用,同步保证了对象只能被一个线程占用,wait保证了当线程在等待过程中释放锁,使得其他对象有机会获得锁。
在一个对象中,用synchonized声明的方法为同步方法。Java中有一个同步模型-监视器,负责管理线程对对象中的同步方法的访问,它的原理是:赋予该对象唯一一把'钥匙',当多个线程进入对象,只有取得该对象钥匙的线程才可以访问同步方法,其它线程在该对象中等待,直到该线程用wait()方法放弃这把钥匙,其它等待的线程抢占该钥匙,抢占到钥匙的线程后才可得以执行,而没有取得钥匙的线程仍被阻塞在该对象中等待。 总而言之,synchonized使得只有一个线程能进入临界代码区。
代码实现:
 
package com.thread;  
  
public class ProducerConsumer {  
    public static void main(String[] args) {  
        ShareData sd = new ShareData();  
        new Producer(sd).start();  
        new Consumer(sd).start();  
    }  
}  
  
class Producer extends Thread{  
      
    private ShareData sd;  
    public Producer(ShareData sd){  
        this.sd = sd;  
    }  
      
    @Override  
    public void run() {  
        for(int i = 0; i < 20; i++){  
            int product = (int)(Math.random()*1000);  
            sd.setArray(product);  
            try {  
                Thread.sleep((int)(Math.random()*200));  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
}  
  
class Consumer extends Thread{  
    private ShareData sd;  
    public Consumer(ShareData sd){  
        this.sd = sd;  
    }  
    @Override  
    public void run() {  
        for(int i = 0; i < 20; i++){  
            sd.getArray();  
            try {  
                Thread.sleep((int)(Math.random()*200));  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
}  
  
class ShareData{  
    private static int shareArray[] = new int[10];  
    private int count;  
    private int in;  
    private int out;  
      
    ShareData(){  
        this.count = 0;  
        this.in = 0;  
        this.out = 0;  
    }  
      
    public synchronized void setArray(int product){  
        try{  
            while(count >= shareArray.length){  
                System.out.println("array full.");  
                this.wait();  
            }  
            this.notify();  
        }catch (Exception e) {  
            e.printStackTrace();  
        }  
        shareArray[in] = product;  
        count++;  
        System.out.println("produce: " + product);  
        in = (in + 1) % shareArray.length;  
    }  
      
    public synchronized int getArray(){  
        try{  
            while(count <= 0){  
                System.out.println("array empty.");  
                this.wait();  
            }  
            this.notify();  
        }catch(Exception e){  
            e.printStackTrace();  
        }  
        int consume = shareArray[out];  
        count--;  
        System.out.println("consume: " + consume);  
        out = (out + 1) % shareArray.length;  
        notify();  
        return consume;  
    }  
}  

 

输出代码(每次都不同):
 
array empty.  
produce: 86  
consume: 86  
array empty.  
produce: 232  
consume: 232  
array empty.  
produce: 438  
consume: 438  
produce: 272  
consume: 272  
array empty.  
produce: 495  
consume: 495  
produce: 354  
produce: 533  
consume: 354  
produce: 92  
consume: 533  
produce: 374  
consume: 92  
produce: 441  
produce: 141  
consume: 374  
consume: 441  
consume: 141  
array empty.  
produce: 68  
consume: 68  
produce: 978  
consume: 978  
array empty.  
produce: 737  
consume: 737  
array empty.  
produce: 904  
consume: 904  
array empty.  
produce: 613  
consume: 613  
array empty.  
produce: 812  
consume: 812  
produce: 726  
produce: 326  
consume: 726  
produce: 305  
consume: 326  
consume: 305  

 

 
 
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,