Consider the following code :
class ThreadJoinTest{
public static void main(String[] arguments){
TimeUnit unit;
final Thread taskThread = new Thread(task);
taskThread.start();
taskThread.join(unit.millis(timeout));
}
}
So when the main thread would execute the line taskThread.join() with a time out value, the main thread is giving the taskThread ample time to finish its task? Is this the main objective of the join method?
What will happen if:
taskThreadfinishes its execution before the time out period?- What happens if the timeout is reached but the
taskThreadhas not finished executing? Does thetaskThreadget a chance to finish its task or not? - How does the main thread know that join returned because the
taskThreadfinished normally or because timeout was reached?
mainwill be allowed to start again as soon astaskThreadisdone.
mainwill be allowed to start again, andtaskThreadwill continue. Both threads will be allowed to finish.taskThreadfinished normally or the timeout is reached main will continue to execute. There is no way formainto know if the timeout occurred or iftaskThreadfinished executing without using some other means of communication.