Java Thread Join


 

Java Thread Join

In this segment of tutorial we will learn how to use the join method in the Thread.Then We will create an example of Thread with the use of join method.

In this segment of tutorial we will learn how to use the join method in the Thread.Then We will create an example of Thread with the use of join method.

  • Java Join method join the next thread at the end of the current thread
  • After current thread stops execution then next thread executes.


Java Join Thread Example

public class join implements Runnable {

	@Override
	public void run() {
		for (int x = 1; x <= 3; x++) {
			System.out.println("this is thread "
					+ Thread.currentThread().getName());
		}
	}

	public static void main(String[] args) throws Exception {
		join j1 = new join();
		Thread t1 = new Thread(j1, "1");
		Thread t2 = new Thread(j1, "2");
		Thread t3 = new Thread(j1, "3");
		t1.start();
		t1.join();
		t2.start();
		t3.start();
	}
}

Output :

this is thread 1 this is thread 1 this is thread 1 this is thread 3 this is thread 2 this is thread 2 this is thread 2 this is thread 3 this is thread 3

Ads