Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java Joining a Thread

Sometimes one thread needs to know when another thread is ending. In java, isAlive() and join() are two different methods to check whether a thread has finished its execution. The isAlive() methods return true if the thread upon which it is called is still running otherwise it return false.

final boolean isAlive()

But, join() method is more popular than isAlive() method. This method causes the currently running threads to stop executing until the thread it joins with completes its task.

final void join() throws InterruptedException

Using join() method, we tell our thread to wait until the specifid thread completes its execution. There are overloaded versions of join() method, which allows us to specify time for which you want to wait for the specified thread to terminate.

Using join() method, we tell our thread to wait until the specifid thread completes its execution. There are overloaded versions of join() method, which allows us to specify time for which you want to wait for the specified thread to terminate.

let's see an example of isAlive() method

public class MyThread extends Thread
{
	public void run()
	{
		System.out.println("Java ");
		try
		{
			Thread.sleep(1000);
		}
		catch(InterruptedException ie)
		{
		}
		System.out.println("JavaRace");
	}
	public static void main(String[] args)
	{
		MyThread t1=new MyThread();
		MyThread t2=new MyThread();
		t1.start();
		t2.start();
		System.out.println(t1.isAlive());
		System.out.println(t2.isAlive());
	}
}

Output

Java
true
true
r1
JavaRace
JavaRace

Let's see an example of thread without join() method

public class MyThread extends Thread
{
	public void run()
	{
		System.out.println("Java ");
		try
		{
		Thread.sleep(1000);
		}
		catch(InterruptedException ie)
		{
		}
		System.out.println("JavaRace ");
	}
	public static void main(String[] args)
	{
		MyThread t1=new MyThread();
		MyThread t2=new MyThread();
		t1.start();
		t2.start();
	}
}

Output

Java
Java
JavaRace
JavaRace

In this above program two thread t1 and t2 are created. t1 starts first and after printing "Java" on console thread t1 goes to sleep for 1000 mls. At the same time Thread t2 will start its process and print "Java" on console and then goes into sleep for 1000 mls. Thread t1 will wake up from sleep and print "JavaRace" on console similarly thread t2 will wake up from sleep and print "JavaRace" on console.

Let's see an example of thread with join() method

public class MyThread extends Thread
{
	public void run()
	{
		System.out.println("Java ");
		try
		{
			Thread.sleep(1000);
		}
		catch(InterruptedException ie)
		{
		}
		System.out.println("JavaRace ");
	}
	public static void main(String[] args)
	{
		MyThread t1=new MyThread();
		MyThread t2=new MyThread();
		t1.start();
		try
		{
			t1.join(); //Waiting for t1 to finish
		}
		catch(InterruptedException ie)
		{
		}
		t2.start();
	}
}

Output

r1
r2
r1
r2
r2

In this above program join() method on thread t1 ensure that t1 finishes it process before thread t2 starts.

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext