Inter-thread communication in java

Inter-thread communication:

Inter-thread communication is a process in which a thread is paused running in its critical region and another thread is allowed to enter (or lock) in the same critical region to be executed. i.e. synchronized threads communicate with each other.

Below object class methods are used for Inter-thread communication process:

1. wait(): this method instructs the current thread to release the monitor held by it and to get suspended until some other threads sends a notification from the same monitor.

Syntax: public void wait() throws InterruptedException. 

2. notify(): this method is used to send the notification to the thread that is suspended by the wait() method.

Syntax: public void notify().

3. notifyAll(): this method is used to send the notification to all the threads that are suspended by wait() method.

Syntax: public void notifyAll().

Producer-consumer problem to understand Inter-thread communication.

Example:

ProducerConsumerExample.java

/**
 * This program is used to show the inter thread communication. 
 * @author w3spoint
 */
class Buffer{
	int a;
	boolean produced = false;
 
	public synchronized void produce(int x){
		if(produced){
			System.out.println("Producer is waiting...");
			try{
				wait();
			}catch(Exception e){
				System.out.println(e);
			}
		}
		a=x;
		System.out.println("Product" + a + " is produced.");
		produced = true;
		notify();		
	}
 
	public synchronized void consume(){
		if(!produced){
			System.out.println("Consumer is waiting...");
			try{
				wait();
			}catch(Exception e){
				System.out.println(e);
			}
		}
		System.out.println("Product" + a + " is consumed.");
		produced = false;
		notify();
	}
}
 
class Producer extends Thread{
	Buffer b;
	public Producer(Buffer b){
		this.b = b;
	}
 
	public void run(){
		System.out.println("Producer start producing...");
		for(int i = 1; i <= 10; i++){
			b.produce(i);
		}
	}
}
 
class Consumer extends Thread{
	Buffer b;
	public Consumer(Buffer b){
		this.b = b;
	}
 
	public void run(){
		System.out.println("Consumer start consuming...");
		for(int i = 1; i <= 10; i++){
			b.consume(); 
		}
	}
}
 
public class ProducerConsumerExample {
	public static void main(String args[]){
		//Create Buffer object.
		Buffer b = new Buffer();
 
		//creating producer thread.
		Producer p = new Producer(b);
 
		//creating consumer thread.
		Consumer c = new Consumer(b);
 
		//starting threads.
		p.start();
		c.start();
	}
}

Output:

Consumer start consuming...
Producer start producing...
Consumer is waiting...
Product1 is produced.
Producer is waiting...
Product1 is consumed.
Consumer is waiting...
Product2 is produced.
Producer is waiting...
Product2 is consumed.
Consumer is waiting...
Product3 is produced.
Producer is waiting...
Product3 is consumed.
Consumer is waiting...
Product4 is produced.
Producer is waiting...
Product4 is consumed.
Consumer is waiting...
Product5 is produced.
Producer is waiting...
Product5 is consumed.
Consumer is waiting...
Product6 is produced.
Producer is waiting...
Product6 is consumed.
Consumer is waiting...
Product7 is produced.
Producer is waiting...
Product7 is consumed.
Consumer is waiting...
Product8 is produced.
Producer is waiting...
Product8 is consumed.
Consumer is waiting...
Product9 is produced.
Producer is waiting...
Product9 is consumed.
Consumer is waiting...
Product10 is produced.
Product10 is consumed.

Download this example.
 
Next Topic: Synchronization in java with example.
Previous Topic: Starvation in java with example.

Content Protection by DMCA.com
Please Share