转载

Java实现生产者消费者模型

Java实现生产者消费者模型

生产者消费者模型,是一般面试题都会考的,下面介绍使用ReetrantLock实现

生产者消费者模型。

定义一个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();
    }
}
正文到此结束
本文目录