题目链接:点击打开链接
题目大意:略。
解题思路:略。
相关企业
- 领英(LinkedIn)
AC 代码
class BoundedBlockingQueue { private final Queue queue; private final int size; private final ReentrantLock lock; private final Condition producer; private final Condition consumer; public BoundedBlockingQueue(int capacity) { size = capacity; queue = new LinkedList(); lock = new ReentrantLock(); producer = lock.newCondition(); consumer = lock.newCondition(); } public void enqueue(int element) throws InterruptedException { try { lock.lock(); while (size() == size) { producer.await(); } queue.add(element); consumer.signalAll(); } finally { lock.unlock(); } } public int dequeue() throws InterruptedException { try { lock.lock(); while (queue.isEmpty()) { consumer.await(); } int result = (int) queue.poll(); producer.signalAll(); return result; } finally { lock.unlock(); } } public int size() { return queue.size(); } }