Design a thread-safe queue with a fixed capacity. Multiple producer and consumer threads share one BoundedBlockingQueue instance.
Implement these operations:
enqueue(element) inserts an element at the back of the queue. If the queue is full, it must block until space becomes available.dequeue() removes and returns the element at the front. If the queue is empty, it must block until an element becomes available.size() returns the current number of elements in the queue.
The queue must preserve first-in, first-out order, never contain more than capacity elements, and coordinate waiting threads without busy-waiting.
The judge creates the queue and starts producer and consumer threads. Your implementation should provide the synchronization inside the queue rather than create threads itself.
The judge also preloads the standard concurrency and collection APIs for each supported language. You do not need to add import, include, or using statements.
Example 1:
Input:
Output:
Explanation: Elements leave the queue in the same order in which they entered.
Example 2:
Input:
Output:
Constraints
1 <= capacity <= 10001 <= element <= 1_000_000enqueue, dequeue, and size may be called concurrently.- The judge uses a finite, balanced collection of enqueue and dequeue operations.