How does the join() method of Thread class work?

  1. main will be allowed to start again as soon as taskThread is done.
  2. Then main will be allowed to start again, and taskThread will continue. Both threads will be allowed to finish.
  3. If either the 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.

Tags:

Java