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?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.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With