-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathConcurrentLinkedDequeExample.java
More file actions
86 lines (78 loc) · 3.5 KB
/
Copy pathConcurrentLinkedDequeExample.java
File metadata and controls
86 lines (78 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package org.alxkm.patterns.queue;
import java.util.concurrent.ConcurrentLinkedDeque;
/**
* Demonstrates the usage of ConcurrentLinkedDeque in a multithreaded environment.
* <p>
* Where multiple threads can safely add and remove elements from the deque without explicit synchronization.
*/
public class ConcurrentLinkedDequeExample {
/**
* We create a ConcurrentLinkedDeque named deque.
* We define two producer threads (producer1 and producer2) that add elements to the deque.
* Each producer thread adds elements to the deque with a delay of 1 second between each addition.
* We define two consumer threads (consumer1 and consumer2) that remove elements from the deque.
* consumer1 removes elements from the front of the deque, while consumer2 removes elements from the end of the deque.
* Each consumer thread removes elements from the deque with a delay of 2 seconds between each removal.
* We start all producer and consumer threads concurrently.
* As elements are added and removed from the deque, the producer and consumer threads print messages indicating the elements they add or remove.
*/
public static void main(String[] args) {
// Create a ConcurrentLinkedDeque
ConcurrentLinkedDeque<Integer> deque = new ConcurrentLinkedDeque<>();
// Create and start producer threads
Thread producer1 = new Thread(() -> {
for (int i = 0; i < 5; i++) {
deque.add(i); // Add elements to the deque
System.out.println("Producer 1 added: " + i);
try {
Thread.sleep(1000); // Simulate production time
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
});
Thread producer2 = new Thread(() -> {
for (int i = 5; i < 10; i++) {
deque.add(i); // Add elements to the deque
System.out.println("Producer 2 added: " + i);
try {
Thread.sleep(1000); // Simulate production time
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
});
// Create and start consumer threads
Thread consumer1 = new Thread(() -> {
while (!deque.isEmpty()) {
int element = deque.poll(); // Remove and return the first element from the deque
System.out.println("Consumer 1 removed: " + element);
try {
Thread.sleep(2000); // Simulate consumption time
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
});
Thread consumer2 = new Thread(() -> {
while (!deque.isEmpty()) {
int element = deque.pollLast(); // Remove and return the last element from the deque
System.out.println("Consumer 2 removed: " + element);
try {
Thread.sleep(2000); // Simulate consumption time
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
});
// Start producer and consumer threads
producer1.start();
producer2.start();
consumer1.start();
consumer2.start();
}
}