Thread class in java provides numerous methods that are very essential in order to understand the working of threads as the thread stages are triggered by them. Java multi-threading provides two ways to find with the help of isAlive() and join() method.
One thread gets to know when another thread has ended. Let us do depict stages of the lifecycle of the thread via the below image which helps us to connect dots to understand these methods’ workings.

Now let us do discuss isAlive() method of Thread class to a deeper depth. Basically, this method works internally very closely in parallel to the lifecycle stages of a thread. 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 class has been called or if the thread has been terminated, we must use the isAlive() method. This method is used to find out if a thread has actually been started and has yet not terminated.
Syntax:
final boolean isAlive()
Return Value: Boolean value returns
Note: While returning this function returns true if the thread upon which it is called is still running. It returns false otherwise.
Example
Java
public class oneThread extends Thread {
public void run()
{
System.out.println( "geeks " );
try {
Thread.sleep( 300 );
}
catch (InterruptedException ie) {
}
System.out.println( "forgeeks " );
}
public static void main(String[] args)
{
oneThread c1 = new oneThread();
oneThread c2 = new oneThread();
c1.start();
c2.start();
System.out.println(c1.isAlive());
System.out.println(c2.isAlive());
}
}
|
Output:
geeks
true
true
geeks
forgeeks
forgeeks
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
09 May, 2022
Like Article
Save Article