How does the join() method of Thread class work?
main
will be allowed to start again as soon astaskThread
is done.- Then
main
will be allowed to start again, andtaskThread
will continue. Both threads will be allowed to finish. - If either the
taskThread
finished normally or the timeout is reached main will continue to execute. There is no way formain
to know if the timeout occurred or iftaskThread
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.