LeetCode 1188. Design Bounded Blocking Queue

Posted Dylan_Java_NYC

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 1188. Design Bounded Blocking Queue相关的知识,希望对你有一定的参考价值。

原题链接在这里:https://leetcode.com/problems/design-bounded-blocking-queue/

题目:

Implement a thread safe bounded blocking queue that has the following methods:

  • BoundedBlockingQueue(int capacity) The constructor initializes the queue with a maximum capacity.
  • void enqueue(int element) Adds an element to the front of the queue. If the queue is full, the calling thread is blocked until the queue is no longer full.
  • int dequeue() Returns the element at the rear of the queue and removes it. If the queue is empty, the calling thread is blocked until the queue is no longer empty.
  • int size() Returns the number of elements currently in the queue.

Your implementation will be tested using multiple threads at the same time. Each thread will either be a producer thread that only makes calls to the enqueue method or a consumer thread that only makes calls to the dequeue method. The size method will be called after every test case.

Please do not use built-in implementations of bounded blocking queue as this will not be accepted in an interview.

 

Example 1:

Input:
1
1
["BoundedBlockingQueue","enqueue","dequeue","dequeue","enqueue","enqueue","enqueue","enqueue","dequeue"]
[[2],[1],[],[],[0],[2],[3],[4],[]]

Output:
[1,0,2,2]

Explanation:
Number of producer threads = 1
Number of consumer threads = 1

BoundedBlockingQueue queue = new BoundedBlockingQueue(2);   // initialize the queue with capacity = 2.

queue.enqueue(1);   // The producer thread enqueues 1 to the queue.
queue.dequeue();    // The consumer thread calls dequeue and returns 1 from the queue.
queue.dequeue();    // Since the queue is empty, the consumer thread is blocked.
queue.enqueue(0);   // The producer thread enqueues 0 to the queue. The consumer thread is unblocked and returns 0 from the queue.
queue.enqueue(2);   // The producer thread enqueues 2 to the queue.
queue.enqueue(3);   // The producer thread enqueues 3 to the queue.
queue.enqueue(4);   // The producer thread is blocked because the queue‘s capacity (2) is reached.
queue.dequeue();    // The consumer thread returns 2 from the queue. The producer thread is unblocked and enqueues 4 to the queue.
queue.size();       // 2 elements remaining in the queue. size() is always called at the end of each test case.

 

Example 2:

Input:
3
4
["BoundedBlockingQueue","enqueue","enqueue","enqueue","dequeue","dequeue","dequeue","enqueue"]
[[3],[1],[0],[2],[],[],[],[3]]

Output:
[1,0,2,1]

Explanation:
Number of producer threads = 3
Number of consumer threads = 4

BoundedBlockingQueue queue = new BoundedBlockingQueue(3);   // initialize the queue with capacity = 3.

queue.enqueue(1);   // Producer thread P1 enqueues 1 to the queue.
queue.enqueue(0);   // Producer thread P2 enqueues 0 to the queue.
queue.enqueue(2);   // Producer thread P3 enqueues 2 to the queue.
queue.dequeue();    // Consumer thread C1 calls dequeue.
queue.dequeue();    // Consumer thread C2 calls dequeue.
queue.dequeue();    // Consumer thread C3 calls dequeue.
queue.enqueue(3);   // One of the producer threads enqueues 3 to the queue.
queue.size();       // 1 element remaining in the queue.

Since the number of threads for producer/consumer is greater than 1, we do not know how the threads will be scheduled in the operating system, even though the input seems to imply the ordering. Therefore, any of the output [1,0,2] or [1,2,0] or [0,1,2] or [0,2,1] or [2,0,1] or [2,1,0] will be accepted.

题解:

Use ReentrantLock lock. For each method, lock.lock() first, and finally lock.unlock().

Two Condition full and empty, while size == capacity, full.await(). Exist while, givs empty signal. Vice versa.

Time Complexity: O(1).

Space: O(n).

AC Java:

 1 import java.util.*;
 2 
 3 class BoundedBlockingQueue {
 4     private ReentrantLock lock = new ReentrantLock();
 5     private Condition full = lock.newCondition();
 6     private Condition empty = lock.newCondition();
 7     private int [] que;
 8     private int head = 0;
 9     private int tail = 0;
10     private int size = 0;
11     
12     public BoundedBlockingQueue(int capacity) {
13         que = new int[capacity];    
14     }
15     
16     public void enqueue(int element) throws InterruptedException {
17         lock.lock();
18         try{
19             while(size == que.length){
20                 full.await();
21             }
22             
23             que[tail++] = element;
24             tail %= que.length;
25             size++;
26             empty.signal();
27         }finally{
28             lock.unlock();
29         }
30     }
31     
32     public int dequeue() throws InterruptedException {
33         lock.lock();
34         try{
35             while(size == 0){
36                 empty.await();
37             }
38             
39             int res = que[head++];
40             head %= que.length;
41             size--;
42             full.signal();
43             return res;
44         }finally{
45             lock.unlock();
46         }
47     }
48     
49     public int size() {
50         lock.lock();
51         try{
52             return this.size;
53         }finally{
54             lock.unlock();
55         }
56     }
57 }

 

以上是关于LeetCode 1188. Design Bounded Blocking Queue的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口

leetcode 355 Design Twitte

LeetCode 706. Design HashMap

LeetCode Design Hit Counter

LeetCode 622. Design Circular Queue

LeetCode设计题 design(共38题)