CodesJava

Easy learning with example program codes

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 codesjava
 */
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.

Related Topics:

Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki