Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Multithreading. No Comments on Inter Thread Communication
2.3
(3)

In this article we will learn how to implement inter thread communication i.e how can we coordinate the communication between two or more threads.

 

Although we can restrict the access of data or code to a single thread at a time by using synchronization, it can’t guarantee the consistent execution of our logic.

 

As an example let’s consider the conventional Producer-Consumer problem which is generally introduced in Operating Systems course. In this problem Producer process or thread produces items and stores in a queue and Consumer process or thread consumes items from the queue. Note that the Consumer thread should wait until the Producer produces at least one item.

 

In our example we will maintain a class Q which contains a single variable n. The Producer thread stores item (integer) into and Consumer thread retrieves the item (integer) from n.


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


 

Consumer thread must wait until the Producer places an item in n and Producer should wait until the Consumer retrieves the item in n. This kind of inter thread communication can be achieved using the following pre-defined methods in Object class:

final void wait() throws InterruptedException

final void notify()

final void notifyall()

 

Consider the following example Java Program which provides solution to Producer-Consumer problem:

class Q
{
	int n;
	synchronized void put(int n)
	{
		this.n = n;
		System.out.println("Put: " + n);
	}
	synchronized int get()
	{
		System.out.println("Got: " + n);
		return n;
	}
}

class Producer implements Runnable
{
	Q q;
	Producer(Q q)
	{
		this.q = q;
		new Thread(this, "Producer").start();
	}
	public void run()
	{
		int i = 0;
		while(true)
		{
			q.put(i++);
		}
	}
}

class Consumer implements Runnable
{
	Q q;
	Consumer(Q q)
	{
		this.q = q;
		new Thread(this, "Consumer").start();
	}
	public void run()
	{
		while(true)
		{
			q.get();
		}
	}
}

public class Driver
{	
	public static void main(String[] args)
	{
		Q q = new Q();
		Producer p = new Producer(q);
		Consumer c = new Consumer(q);
	}
}

 

Although the put() and get() methods are sychronized, the output of the above program looks like this:

Put: 1

Put: 2

Put: 3

Got: 3

Got: 3

Got: 3

Put: 4

Put: 5

Put: 6

Got: 6

Got: 6

…

 

To make the Producer wait until Consumer retrieves the item and Consumer wait until Producer places an item, we can use the wait() and notify() methods as shown in the below program:

class Q
{
	int n;
	boolean statusFlag = false;
	synchronized void put(int n)
	{
		try
		{
			while(statusFlag)
			{
				wait();
			}
		}
		catch(InterruptedException e){}
		this.n = n;
		System.out.println("Put: " + n);
		statusFlag = true;
		notify();
	}
	synchronized int get()
	{
		try
		{
			while(!statusFlag)
			{
				wait();
			}
		}
		catch(InterruptedException e){}
		statusFlag = false;
		System.out.println("Got: " + n);
		notify();
		return n;
	}
}

class Producer implements Runnable
{
	Q q;
	Producer(Q q)
	{
		this.q = q;
		new Thread(this, "Producer").start();
	}
	public void run()
	{
		int i = 0;
		while(true)
		{
			q.put(i++);
		}
	}
}

class Consumer implements Runnable
{
	Q q;
	Consumer(Q q)
	{
		this.q = q;
		new Thread(this, "Consumer").start();
	}
	public void run()
	{
		while(true)
		{
			q.get();
		}
	}
}

public class Driver
{	
	public static void main(String[] args)
	{
		Q q = new Q();
		Producer p = new Producer(q);
		Consumer c = new Consumer(q);
	}
}

 

Output of the above program is:

Put: 1

Got: 1

Put: 2

Got: 2

Put: 3

Got: 3

…

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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