Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Video lesson

Creating Multiple Thread, isAlive() and Join() Method of Thread in Java

 

In this lesson, you will learn.

  • isAlive() and join() Methods of a Thread Class
  • Example

 

Background

  • Sometimes, we want the main() thread to finish last.
  • In the previous lesson example, we added a sleep(1000) method in main() to ensure that the main() will finish its execution after its all child thread.
  • However, it is not a good solution and it raises a big question.
  • How does one thread know when another thread has ended? This can be understood by the following methods.

 

isAlive() and join() Method

The isAlive() and join() methods are defined in the Thread class.

1. isAlive(): This method returns true if the thread upon which it is called is still running. It returns false if the thread has finished running or has not yet started.

2. join(): This method causes the calling thread (e.g., the main thread) to wait until the thread on which join() is called finishes its execution.

If the thread has already finished, join() return immediately.

 

Example: isAlive() and join() Method

package isaliveandjoin;

class MyThread extends Thread {
	public void run() {
		System.out.println("Thread started: " 
				+Thread.currentThread().getName());
		try {
			// Simulating work by sleeping for 2 seconds
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			System.out.println("Thread interrupted: " 
					+Thread.currentThread().getName());
		}
		System.out.println("Thread finished: " 
				+Thread.currentThread().getName());
	}
}
public class ThreadMethodExample {
	public static void main(String[] args) {
		MyThread thread = new MyThread();
		thread.start();  // Start the thread  
		// Check if thread is alive
		System.out.println("Is thread alive? " + thread.isAlive());

		try {
			// Main thread waits for 'thread' to finish
			thread.join();
		} catch (InterruptedException e) {
			System.out.println("Main thread interrupted while 
                  waiting for completion.");
		}
		// Check again if thread is alive after join
		System.out.println("Is thread alive after join? " + thread.isAlive()); 
		System.out.println("Main Thread Exiting......");
	}
}

Output

Thread started: Thread-0
Is thread alive? true
Thread finished: Thread-0
Is thread alive after join? false
Main Thread Exiting......

Explanation

  • MyThread Class: This is a custom thread class that extends Thread. In its run() method, it simulates some work by sleeping for 2 seconds.
  • ThreadMethodExample: Here, an instance of MyThread is created and started. The isAlive() method is called to check if the thread is running.
  • After that, the join() method is called on the thread instance which makes the main thread wait until MyThread finishes its execution.
  • After the join() call, isAlive() is checked again to confirm that the thread has stopped running.

This example shows how to manage thread execution using isAlive() to check the thread’s status and join() to synchronize threads by making one thread wait for another to finish.

As you can see, after the calls to join( ) return, the threads have stopped executing.

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

5.0
5.0 out of 5 stars (based on 1 review)
Excellent100%
Very good0%
Average0%
Poor0%
Terrible0%

 

 

03/06/2025

great teaching

 

 

Submit a Review