isAlive() and join() methods of Thread Class in Java
How can one thread know when another thread has ended?
Thread provides a means by which you can answer this question. Java multi-threading provides two ways to find that
- isAlive() : It tests if this thread is alive. A thread is alive if it has been started and has not yet died. There is a transitional period from when a thread is running to when a thread is not running. After the run() method returns, there is a short period of time before the thread stops. If we want to know if the start method of the thread has been called or if thread has been terminated, we must use isAlive() method. This method is used to find out if a thread has actually been started and has yet not terminated.
General Syntax :
final boolean isAlive( ) Return Value: returns true if the thread upon which it is called is still running. It returns false otherwise.
- join() : When the join() method is called, the current thread will simply wait until the thread it is joining with is no longer alive.
Or we can say the method that you will more commonly use to wait for a thread to finish is called join( ). This method waits until the thread on which it is called terminates. Its name comes from the concept of the calling thread waiting until the specified thread joins it. Additional forms of join( ) allow you to specify a maximum amount of time that you want to wait for the specified thread to terminate.
Syntax :final void join( ) throws InterruptedException
Here is an improved version of the preceding example that uses join( ) to ensure that the main thread is the last to stop. It also demonstrates the isAlive( ) method.
// Java program to illustrate// isAlive()publicclassoneThreadextendsThread {publicvoidrun(){System.out.println("geeks ");try{Thread.sleep(300);}catch(InterruptedException ie) {}System.out.println("forgeeks ");}publicstaticvoidmain(String[] args){oneThread c1 =newoneThread();oneThread c2 =newoneThread();c1.start();c2.start();System.out.println(c1.isAlive());System.out.println(c2.isAlive());}}chevron_rightfilter_noneOutput:
geeks true true geeks forgeeks forgeeks
Example of thread without join()
// Java program to illustrate// thread without join()publicclassoneThreadextendsThread {publicvoidrun(){System.out.println("geeks ");try{Thread.sleep(500);}catch(InterruptedException ie) {}System.out.println("forgeeks ");}publicstaticvoidmain(String[] args){oneThread c1 =newoneThread();oneThread c2 =newoneThread();c1.start();c2.start();}}chevron_rightfilter_noneOutput:
geeks geeks forgeeks forgeeks
Example of thread with join()
// Java program to illustratepublicclassoneThreadextendsThread {publicvoidrun(){System.out.println("geeks ");try{Thread.sleep(300);}catch(InterruptedException ie) {}System.out.println("forgeeks ");}publicstaticvoidmain(String[] args){oneThread c1 =newoneThread();oneThread c2 =newoneThread();c1.start();try{c1.join();// Waiting for c1 to finish}catch(InterruptedException ie) {}c2.start();}}chevron_rightfilter_noneOutput:
geeks forgeeks geeks forgeeks
This article is contributed by Shivani Ghughtyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Differences between wait() and join() methods in Java
- Java Concurrency - yield(), sleep() and join() methods
- Java.lang.Thread class in Java
- java.math class and its methods | Set 3
- java.math class and its methods | Set 1
- Java.io.OutputStreamWriter Class methods
- Java.util.BitSet class methods in Java with Examples | Set 2
- java.lang.Boolean class methods
- BitSet class methods in Java with Examples | Set 3
- java.lang.Character class - methods | Set 2
- Math class methods in Java with Examples | Set 2
- Java.lang.Integer class and its methods
- java.lang.Character class methods | Set 1
- getproperty() and getproperties() methods of System Class in Java
- util.date class methods in Java with Examples



