join() is a instance method of java.lang.Thread class which we can use join() method to ensure all threads that started from main must end in order in which they started and also main should end in last. In other words waits for this thread to die.
join() method throws InterruptedException.
When join() method is called on thread it goes from running to waiting state. And wait for thread to die.
Thread need not to acquire object lock before calling join() method i.e. join() method can be called from outside synchronized block.
Waits for this thread to die.
This method internally calls join(0). And timeout of 0 means to wait forever;
join(long millis) – synchronized method
Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
Example of join method
calling thread1.join() made main thread to wait until Thread-1 dies.
Let’s check a program to use join(long millis)
First, join(1000) will be called on Thread-1, but once 1000 millisec are up, main thread can resume and start thread2 (main thread won’t wait for Thread-1 to die).