-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJoining.java
More file actions
79 lines (61 loc) · 1.62 KB
/
Joining.java
File metadata and controls
79 lines (61 loc) · 1.62 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 concurrency;
import java.io.*;
import java.util.concurrent.*;
import net.mindview.util.*;
/**
* RUN:
* javac concurrency/Joining.java && java concurrency.Joining
*
* OUTPUT:
* Grumpy was interrupted isInterrupted(): false
* Doc join completed
* Sleepy working
* Dopey join completed
*/
public class Joining {
public static void main(String[] args) {
Sleeper sleepy = new Sleeper("Sleepy", 2500);
Sleeper grumpy = new Sleeper("Grumpy", 2500);
Joiner dopey = new Joiner("Dopey", sleepy);
Joiner doc = new Joiner("Doc", grumpy);
grumpy.interrupt();
}
}
class Sleeper extends Thread {
private int duration;
public Sleeper(String name, int sleepTime) {
super(name);
duration = sleepTime;
start();
}
public void run() {
try {
sleep(duration);
}
catch (InterruptedException e) {
System.out.println(
getName() + " was interrupted "
+ " isInterrupted(): "+isInterrupted()
);
return;
}
System.out.println(getName() + " working");
}
}
class Joiner extends Thread {
private Sleeper sleeper;
public Joiner(String name, Sleeper sleeper) {
super(name);
this.sleeper = sleeper;
start();
}
public void run() {
try {
sleeper.join();
}
catch (InterruptedException e) {
System.out.println(getName() +" interrupted");
}
System.out.println(getName() +" join completed");
}
}