Java实现生产者消费者模型

更新时间:2020-04-16 10:28:28 点击次数:1111次
定义一个ReentrantLock锁,同时new出两个condition,一个控制队满,一个
控制队空
//生产者 消费者
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.locks.*;
import java.util.Random;

class Producer implements Runnable{
    public static Random random = new Random(7777);
    public ReentrantLock lock;
    public Condition full;
    public Condition empty;
    public Queue<Integer> queue;
    public int maxSize;
    Producer(ReentrantLock lock, Condition full,Condition empty, Queue<Integer> queue, int maxSize){
        this.lock = lock; this.queue = queue;
        this.full = full; this.empty = empty;
        this.maxSize = maxSize;
    }
    public void run() {
        try{
            while(true) {
                lock.lock();
                while(queue.size() ==  maxSize) full.await();
                int number = random.nextInt(100);
                queue.offer(number);
                System.out.println(Thread.currentThread().toString() + "produce:   " + String.valueOf(number));
                empty.signalAll();
                lock.unlock();
                Thread.sleep(3000);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}

class Consumer implements Runnable{
    ReentrantLock lock;
    Condition full;
    Condition empty;
    Queue<Integer> queue;
    public Consumer(ReentrantLock lock, Condition full, Condition empty, Queue<Integer> queue) {
        this.lock = lock; this.full = full;
        this.empty = empty; this.queue = queue;
    }

    public void run() {
        try{
            while(true) {
                lock.lock();
                while(queue.size() == 0) empty.await();
                int number = queue.poll();
                System.out.println(Thread.currentThread().toString() + "Consumer: " + String.valueOf(number));
                full.signalAll();
                lock.unlock();
                Thread.sleep(2000);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}
public class Main{
    public static void main(String []args) {
        ReentrantLock lock = new ReentrantLock();
        Condition proCondition = lock.newCondition();
        Condition conCondition = lock.newCondition();
        LinkedList<Integer> queue = new LinkedList<>();
        ExecutorService executor = Executors.newFixedThreadPool(4);

        executor.submit(new Producer(lock, conCondition, proCondition, queue, 10));
        executor.submit(new Producer(lock, conCondition, proCondition, queue, 10));
        executor.submit(new Consumer(lock, conCondition, proCondition, queue));
        executor.submit(new Consumer(lock, conCondition, proCondition, queue));
        try{
            Thread.sleep(60000);
        }catch(Exception e) {
            e.printStackTrace();
        }
        executor.shutdown();
    }
}

本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是一个个人学习交流的平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽,造成漏登,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

回到顶部
嘿,我来帮您!