2015-04-20 15:36:01 -07:00
|
|
|
//: concurrency/Joining.java
|
|
|
|
// Understanding join().
|
|
|
|
import static net.mindview.util.Print.*;
|
|
|
|
|
|
|
|
class Sleeper extends Thread {
|
|
|
|
private int duration;
|
|
|
|
public Sleeper(String name, int sleepTime) {
|
|
|
|
super(name);
|
|
|
|
duration = sleepTime;
|
|
|
|
start();
|
|
|
|
}
|
2015-05-05 11:20:13 -07:00
|
|
|
@Override
|
2015-04-20 15:36:01 -07:00
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
sleep(duration);
|
|
|
|
} catch(InterruptedException e) {
|
|
|
|
print(getName() + " was interrupted. " +
|
|
|
|
"isInterrupted(): " + isInterrupted());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
print(getName() + " has awakened");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Joiner extends Thread {
|
|
|
|
private Sleeper sleeper;
|
|
|
|
public Joiner(String name, Sleeper sleeper) {
|
|
|
|
super(name);
|
|
|
|
this.sleeper = sleeper;
|
|
|
|
start();
|
|
|
|
}
|
2015-05-05 11:20:13 -07:00
|
|
|
@Override
|
2015-04-20 15:36:01 -07:00
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
sleeper.join();
|
|
|
|
} catch(InterruptedException e) {
|
|
|
|
print("Interrupted");
|
|
|
|
}
|
|
|
|
print(getName() + " join completed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Joining {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Sleeper
|
|
|
|
sleepy = new Sleeper("Sleepy", 1500),
|
|
|
|
grumpy = new Sleeper("Grumpy", 1500);
|
|
|
|
Joiner
|
|
|
|
dopey = new Joiner("Dopey", sleepy),
|
|
|
|
doc = new Joiner("Doc", grumpy);
|
|
|
|
grumpy.interrupt();
|
|
|
|
}
|
|
|
|
} /* Output:
|
|
|
|
Grumpy was interrupted. isInterrupted(): false
|
|
|
|
Doc join completed
|
|
|
|
Sleepy has awakened
|
|
|
|
Dopey join completed
|
|
|
|
*///:~
|