forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWaitingThread.java
More file actions
65 lines (55 loc) · 1.6 KB
/
WaitingThread.java
File metadata and controls
65 lines (55 loc) · 1.6 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
package modern.challenge;
public class WaitingThread {
private static final Thread t1 = new CodeT1();
public void waitingThread() {
t1.start();
}
private static class CodeT1 extends Thread {
@Override
public void run() {
Thread t2 = new Thread(new CodeT2());
t2.start();
try {
t2.join();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
// log ex
}
}
}
private static class CodeT2 implements Runnable {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
// log ex
}
System.out.println("WaitingThread t1: " + t1.getState() + " \n");
}
}
}
/*
public void waitingThread() {
new Thread(() -> {
Thread t1 = Thread.currentThread();
Thread t2 = new Thread(() -> {
try {
Thread.sleep(2000);
System.out.println("WaitingThread t1: " + t1.getState() + "\n");
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
// log ex
}
});
t2.start();
try {
t2.join();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
// log ex
}
}).start();
}
*/