Reputation: 27193
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:
taskThread
finishes its execution before the time out period?taskThread
has not finished executing? Does the taskThread
get a chance to finish its task or not?taskThread
finished normally or because timeout was reached?Upvotes: 6
Views: 6753
Reputation: 33534
join()
when called on the thread, will wait for that thread to die (ie for the run method of that thread to get done with..). Only then the line below the join()
will execute. But giving a timeout within join(), will make the join() effect to be nullified after the specific timeout.
Though the timeout occurs, the taskThread will be allowed to finish the work.
Upvotes: 4
Reputation: 1115
The timeout condition will take precedence. When the timeout is reached, the main thread and taskThread are equally probable candidates to execute.
Upvotes: 1
Reputation: 46219
main
will be allowed to start again as soon as taskThread
is
done.main
will be allowed to start again, and taskThread
will continue. Both threads will be allowed to finish.taskThread
finished normally or the timeout is reached main will continue to execute. There is no way for main
to know if the timeout occurred or if taskThread
finished executing without using some other means of communication.Upvotes: 6