Advertisement

手撕生产者-消费者模式 | P类问题与NP类问题

阅读量:

目录

  • 对BlockingQueue进行手动实现
    • 生产者与消费者交互模式
    • P类问题与NP类问题

手撕BlockingQueue实现与分析

复制代码
    public class RequestQueue {
    private final Queue<Request> queue = new LinkedList<>();
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    public  Request getRequest() {
        try {
            lock.lock();
            while (queue.peek() == null) {
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

全部评论 (0)

还没有任何评论哟~