Java Reference
In-Depth Information
public double getResult() {
return result;
}
public double calculate() {
// ... calculate a value for "result"
}
}
class ShowJoin {
public static void main(String[] args) {
CalcThread calc = new CalcThread();
calc.start();
doSomethingElse();
try {
calc.join();
System.out.println("result is "
+ calc.getResult());
} catch (InterruptedException e) {
System.out.println("No answer: interrupted");
}
}
// ... definition of doSomethingElse ...
}
First, a new thread type, CalcThread , is defined to calculate a result.
We start a CalcThread , do something else for a while, and then join
that thread. When join returns, CalcThread.run is guaranteed to have
finished, and result will be set. If CalcThread is already finished when
doSomethingElse has completed, join returns immediately. When a thread
dies, its Thread object doesn't go away, so you can still access its state.
You are not required to join a thread before it can terminate.
Two other forms of join take time-out values analogous to wait . Here
are the three forms of join :
 
Search WWH ::




Custom Search