Java Thread join example and Explanation
The Java Thread join method is used to pause the current Thread until the end of the join operation. Java has three overloaded join methods:
Public final void join (): This method changes the current thread to wait until the end of the thread that executes the join operation. If the thread is interrupted during execution, InterruptedException is thrown.
Public final synchronized void join (long millis): This method changes the current thread to wait until the thread that executes the join operation ends or waits for millis time after the join operation is executed. Because Thread Scheduling depends on the implementation of the operating system, it cannot guarantee that the current thread will change to RUnnable at millis time.
Public final synchroinzed void join (long millis, int nanos): This method changes the current thread to wait until the thread that executes the join operation ends or waits for millis + nanos time after the join operation.
The following example shows the usage of the Thread join method. In this program, ensure that the main thread is the last completed thread, and ensure that the third thread (third thread) is executed only after the first thread ends.
= Thread( MyRunnable(), "t1"= Thread( MyRunnable(), "t2"= Thread( MyRunnable(), "t3" 2000 "All threads are dead, exiting main thread"
MyRunnable "Thread started:::"+4000"Thread ended:::"+
The program output result is as follows:
Thread started: t1
Thread ended: t1
Thread started: t2
Thread started: t3
Thread ended: t2
Thread ended: t3
All threads are dead, exiting main thread
Link: http://www.journaldev.com/1024/java-thread-join-example-with-explanation