Skip to content

Commit 2fce887

Browse files
committed
feat: add BlockingQueueWorkingThreadSafe demonstrating producer-consumer pattern and thread-safe queues
Implemented BlockingQueueWorkingThreadSafe.java to showcase how BlockingQueue ensures safe communication between producer and consumer threads using put() and take(). Includes examples of ArrayBlockingQueue, LinkedBlockingQueue, PriorityBlockingQueue, and SynchronousQueue with concurrency characteristics and blocking behavior explained. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 96b72e5 commit 2fce887

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

Section 25 Collections Frameworks/Queue Interface/Blocking Queue/src/BlockingQueueWorkingThreadSafe.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,9 @@ public void run() {
4949
public class BlockingQueueWorkingThreadSafe {
5050
public static void main(String[] args) {
5151
BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(5);
52-
// thread-safe queue
53-
// wait for queue to become non-empty / wait for space
54-
// simplify concurrency problems like producer-consumer
55-
// standard queue --> immediately
56-
// empty --> remove ( no waiting )
57-
// full --> add ( no waiting )
58-
// Blocking queue
59-
// put --> Blocks if the queue is full until space becomes available
60-
// take --> Blocks if the queue is empty until an element becomes available
61-
// offer --> Waits for space to become available, up to the specified timeout
52+
// BlockingQueue is interfaced so we cannot instantiate, so we wrote here implementation of ArrayBlockingQueue
53+
// A bounded, blocking Queue backed by an array.
6254

63-
// A bounded, blocking queue backed by circular array
64-
// low memory overhead
65-
// uses a single lock for both enqueue and dequeue operations
66-
// more threads --> problem
6755
Thread producer = new Thread(new Producer(queue));
6856
Thread consumer = new Thread(new Consumer(queue));
6957
producer.start();
@@ -88,4 +76,23 @@ public static void main(String[] args) {
8876
// each insert operation must wait for a corresponding remove operation by another thread and vice versa.
8977
// it cannot store elements, capacity of at most one element
9078
}
91-
}
79+
}
80+
81+
/*
82+
thread-safe queue
83+
wait for queue to become non-empty / wait for space
84+
simplify concurrency problems like producer-consumer
85+
standard queue --> immediately
86+
empty --> remove ( no waiting )
87+
full --> add (no waiting)
88+
89+
Blocking queue
90+
put --> Blocks if the queue is full until space becomes available
91+
take --> Blocks if the queue is empty until an element becomes available
92+
offer --> Waits for space to become available, up to the specified timeout
93+
94+
A bounded, blocking queue backed by a circular array low memory overhead uses a single lock
95+
for both enqueue and dequeue operations
96+
97+
more threads --> problem
98+
*/

0 commit comments

Comments
 (0)