Krivalar Tutorials 
Krivalar Tutorials

Java Multithreading - join() method






Java Multithreading - join method

When join() is invoked on a particular thread, the current thread waits for the invoked thread to terminate. Once the invoked thread dies, the current thread continues its execution

Consider the below example when multiple threads execute and join() method is NOT used

Java Thread Example - Threads without join

public class ThreadWithoutJoin implements Runnable{
	String threadName;
	int sum=0;
	public ThreadWithoutJoin(String name){
		this.threadName=name;

	}
	public void run(){
			try{
			Thread.sleep(1000);
			}
			catch(InterruptedException ie){
				ie.printStackTrace();
			}
			for(int i=0;i<1000;i++){
				sum=sum+i;
			}
	}

	public void printSum(){
		System.out.println(threadName+ ":"+sum);

	}

	public static void main(String a[]) {
		ThreadWithoutJoin te1= new ThreadWithoutJoin("Thread1");
		ThreadWithoutJoin te2= new ThreadWithoutJoin("Thread2");
		ThreadWithoutJoin te3= new ThreadWithoutJoin("Thread3");
		ThreadWithoutJoin te4= new ThreadWithoutJoin("Thread4");
		Thread thread1=new Thread(te1);
		Thread thread2=new Thread(te2);
		Thread thread3=new Thread(te3);
		Thread thread4=new Thread(te4);
		thread1.start();
		thread2.start();
		thread3.start();
		thread4.start();

		te1.printSum();
		te2.printSum();
		te3.printSum();
		te4.printSum();

	}

}

Output - ThreadWithoutJoin

Thread1:0
Thread2:0
Thread3:0
Thread4:0

In the above example, main thread terminates before the 4 invoked threads thread1, thread2, thread3 and thread4 complete calculating the sum and hence the result expected is not printed. If Thread join() method is called on each of the instance, the main thread waits until each of invoked instance completes the calculation and terminates.

Java Thread Example - MultiThreading using join

public class ThreadJoinExample implements Runnable{
	String threadName;
	int sum=0;
	public ThreadJoinExample(String name){
		this.threadName=name;

	}
	public void run(){
		try{
		Thread.sleep(1000);
		}
		catch(InterruptedException ie){
			ie.printStackTrace();
		}
		for(int i=0;i<1000;i++){
			sum=sum+i;
		}
	}


	public void printSum(){
		System.out.println(threadName+ ":"+sum);

	}

	public static void main(String a[]) {
		ThreadJoinExample te1= new ThreadJoinExample("Thread1");
		ThreadJoinExample te2= new ThreadJoinExample("Thread2");
		ThreadJoinExample te3= new ThreadJoinExample("Thread3");
		ThreadJoinExample te4= new ThreadJoinExample("Thread4");
		Thread thread1=new Thread(te1);
		Thread thread2=new Thread(te2);
		Thread thread3=new Thread(te3);
		Thread thread4=new Thread(te4);
		thread1.start();
		thread2.start();
		thread3.start();
		thread4.start();

		try {
			thread1.join();
			thread2.join();
			thread3.join();
			thread4.join();

		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		te1.printSum();
		te2.printSum();
		te3.printSum();
		te4.printSum();
	}

}

Output - MultiThreading using join

Thread1:499500
Thread2:499500
Thread3:499500
Thread4:499500




















Searching using Binary Search Tree