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
49 lines (45 loc) · 1.52 KB
/
App.java
File metadata and controls
49 lines (45 loc) · 1.52 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
package WaitAndNotify_8;
/**
* {@link Object#wait()} and {@link Object#notify()} in Java; low-level
* multi-threading methods of the {@link java.lang.Object} class
* that allow you to have one or more threads sleeping, only to be woken up by
* other threads at the right moment. Extremely useful for avoiding those
* processor-consuming "polling loops".
* <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 <[email protected]>
*/
public class App {
public static void main(String[] args) throws InterruptedException {
final Processor processor = new Processor();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
processor.produce();
} catch (InterruptedException ignored) {}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
processor.consume();
} catch (InterruptedException ignored) {}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
}
}