Inter-Thread Communication in Java – Part 2 : Free Java Tutorials

Inter-Thread Communication encompasses those Java concepts and features using which threads can communicate with each other. This communication helps to reduce the unnecessary waiting time of threads. Thus, it helps to improve overall performance.

Two threads can communicate with each other by using wait(), notify() and notifyAll() methods.

In this tutorial, we will see an example of Inter-Thread Communication using these methods.

Example of Inter-Thread Communication

class ChildThread extends Thread 
{	
	// Initially counter is 0
	int cnt = 0;
	public void run()
	{
		synchronized(this)
		{
			System.out.println("Child Thread starts its execution by incrementing counter");
			for(int i=0;i<1000;i++)
			{
				cnt = cnt + 1;
			}
			System.out.println("Child Thread now gives notification to Main Thread");
			this.notify();
		}
	}	
}

public class Demo 
{
	public static void main(String[] args) throws InterruptedException
	{
		ChildThread child_thread = new ChildThread();
		child_thread.start();
		
		synchronized(child_thread)
		{
			System.out.println("Main Thread calls wait method
 on Child Thread and enters into the waiting state");
			child_thread.wait();
			System.out.println("Main Thread received notification from Child Thread");
			
			System.out.println("Final Value of counter: " + child_thread.cnt);
		}
	}
}

In the above code, first both the threads start execution concurrently.

The main thread then calls wait() method on the object of child thread and enters into the waiting state.

Child thread then starts its execution and increments the counter value by 1 for thousand times. This operation is performed in the synchronized block.

After this, child thread notifies main thread about the updated value of counter by calling notify() method.

Main thread receives this notification from child thread and it prints the final value of the counter which is 1000.

Output:

Main Thread calls wait method on Child Thread and enters into the waiting state
Child Thread starts its execution by incrementing counter
Child Thread now gives notification to Main Thread
Main Thread received notification from Child Thread
Final Value of counter: 1000

Instead of using wait() and notify() method, we can use sleep() and join() methods also. This also serves our purpose.

But this practice is not feasible because of the following reasons:

We can make the main thread to sleep for certain time and allow child thread to execute first so that main thread will always get the final value of the counter after successful updation.

This approach is not feasible because calling sleep method for a certain amount of time causes main thread to unnecessarily wait even after successful updation of the counter as we don’t know the exact time that it would take to update the value of counter.

Main thread can also use join() method on child thread object so that child thread will finish its execution first and main thread will always get updated value of the counter.

But, join() method will wait for the entire execution of child thread to get finished. This may not be feasible when child thread has thousands of lines of code and only a few lines are doing updation that is actually required.

Difference between notify() and notifyAll() method

We can use notify() method to give the notification to only one waiting thread. If multiple threads are waiting, then only one thread will be notified and the remaining threads have to wait for further notifications.

Furthermore, we cannot predict which thread will be notified as it depends on JVM.

We can use notifyAll() method to give the notification to all the waiting threads of a particular object.

Even though multiple threads will get the notification, all the threads will execute one by one only. Because threads require the lock in order to execute and only one lock is available for a particular object.

This is all about Inter-Thread Communication in Java. Hope you find this tutorial helpful.

Leave a Comment

Your email address will not be published. Required fields are marked *