-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissedNotify.java
More file actions
79 lines (63 loc) · 1.65 KB
/
Copy pathMissedNotify.java
File metadata and controls
79 lines (63 loc) · 1.65 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
package threadbook.ch08;
public class MissedNotify extends Object {
private Object proceedLock;
public MissedNotify() {
print("in MissedNotify()");
proceedLock = new Object();
}
public void waitToProceed() throws InterruptedException {
print("in waitToProceed() - entered");
synchronized ( proceedLock ) {
print("in waitToProceed() - about to wait()");
proceedLock.wait();
print("in waitToProceed() - back from wait()");
}
print("in waitToProceed() - leaving");
}
public void proceed() {
print("in proceed() - entered");
synchronized ( proceedLock ) {
print("in proceed() - about to notifyAll()");
proceedLock.notifyAll();
print("in proceed() - back from notifyAll()");
}
print("in proceed() - leaving");
}
private static void print(String msg) {
String name = Thread.currentThread().getName();
System.out.println(name + ": " + msg);
}
public static void main(String[] args) {
final MissedNotify mn = new MissedNotify();
Runnable runA = new Runnable() {
public void run() {
try {
Thread.sleep(1000);
mn.waitToProceed();
} catch ( InterruptedException x ) {
x.printStackTrace();
}
}
};
Thread threadA = new Thread(runA, "threadA");
threadA.start();
Runnable runB = new Runnable() {
public void run() {
try {
Thread.sleep(500);
mn.proceed();
} catch ( InterruptedException x ) {
x.printStackTrace();
}
}
};
Thread threadB = new Thread(runB, "threadB");
threadB.start();
try {
Thread.sleep(10000);
} catch ( InterruptedException x ) {
}
print("about to invoke interrupt() on threadA");
threadA.interrupt();
}
}