forked from Beerkay/JavaMultiThreading
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
90 lines (81 loc) · 3.19 KB
/
App.java
File metadata and controls
90 lines (81 loc) · 3.19 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
87
88
89
90
package ProducerConsumer_7;
/**
* Producer-Consumer pattern in Java using the {@link java.util.concurrent
* .ArrayBlockingQueue} Java class.
* <br><br>
* Producer-Consumer is the situation where one or more threads are producing
* data items and adding them to a shared data store of some kind while one or
* more other threads process those items, removing them from the data store.
* <br><br>
* Codes with minor comments are from
* <a href="http://www.caveofprogramming.com/youtube/">
* <em>http://www.caveofprogramming.com/youtube/</em>
* </a>
* <br>
* also freely available at
* <a href="https://www.udemy.com/java-multithreading/?couponCode=FREE">
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
* </a>
*
* @author Z.B. Celik <celik.berkay@gmail.com>
*/
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
@SuppressWarnings("InfiniteLoopStatement")
public class App {
/**
* Thread safe implementation of {@link java.util.Queue} data structure so
* you do not need to worry about synchronization.
* More specifically {@link java.util.concurrent.BlockingQueue}
* implementations are thread-safe. All queuing methods are atomic in nature
* and use internal locks or other forms of concurrency control. If
* BlockingQueue is not used queue is shared data structure either
* {@code synchronized} or {@code wait() notify()} (see Course 8) should be
* used.
* Java 1.5 introduced a new concurrency library {@link java.util.concurrent}
* which was designed to provide a higher level abstraction over
* the wait/notify mechanism.
*/
private static BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10);
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new Runnable() {
public void run() {
try {
producer();
} catch (InterruptedException ignored) {}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
consumer();
} catch (InterruptedException ignored) {}
}
});
t1.start();
t2.start();
// t1.join();
// t2.join();
// Pause for 30 seconds and force quitting the app (because we're
// looping infinitely)
Thread.sleep(30000);
System.exit(0);
}
private static void producer() throws InterruptedException {
Random random = new Random();
while (true) {//loop indefinitely
queue.put(random.nextInt(100));//if queue is full (10) waits
}
}
private static void consumer() throws InterruptedException {
Random random = new Random();
while (true) {
Thread.sleep(100);
if (random.nextInt(10) == 0) {
Integer value = queue.take();//if queue is empty waits
System.out.println("Taken value: " + value + "; Queue size is: " + queue.size());
}
}
}
}