Inter-Thread Communication
It is a technique through which multiple threads communicate with each other.
More than one thread communicates with each other by reducing CPU idle time
If threads A and B communicate with each other when they have completed their
tasks, they do not have to wait and check each other’s status every time.
When more than one threads are executing simultaneously, sometimes they need
to communicate with each other by exchanging information with each other. A
thread exchanges information before or after it changes its state.
Why communication
There are several situations where communication between threads is important.
For example, suppose that there are two threads A and B. Thread B uses data produced by Thread A
and performs its task.
If Thread B waits for Thread A to produce data, it will waste many CPU cycles. But if threads A and B
communicate with each other when they have completed their tasks, they do not have to wait and
check each other’s status every time.
Method Used in inter thread communication
1.wait()
2.notify()
3.notifyAll()
wait(): This method is used to make the particular Thread wait until it gets a notification. This method
pauses the current thread to the waiting room dynamically.
notify(): This method is used to send the notification to one of the waiting thread so that thread enters
into a running state and execute the remaining task. This method wakeup a single thread into the active
state (that acts on the common object).
notifyAll(): This method is used to send the notification to all the waiting threads so that all thread
enters into the running state and execute simultaneously. This method wakes up all the waiting threads
that act on the common objects.
producer-consumer problem
The producer-consumer problem is a classic concurrency problem where one thread, the
producer, generates data, and another thread, the consumer, consumes that data. The challenge
lies in coordinating these threads to ensure that the producer doesn't produce data faster than the
consumer can consume it, and vice versa.
Solutions to the Producer-Consumer Problem in
Java
Java provides several mechanisms to solve the producer-consumer problem:
1. Using wait() and notify():
Producer:
1. Checks if the buffer is full.
2. If full, waits until the consumer consumes an item.
3. If not full, produces an item and notifies the consumer.
Consumer:
1. Checks if the buffer is empty.
2. If empty, waits until the producer produces an item.
3. If not empty, consumes an item and notifies the producer.
Example : Producer and Consumer
class Buffer{ Product1 is produced.
int a; Product1 is consumed.
boolean produced = false; Product2 is produced.
Product2 is consumed.
public synchronized void produce(int x){ Product3 is produced.
if(produced){ Product3 is consumed.
Product4 is produced.
try{ Product4 is consumed.
wait(); Product5 is produced.
}catch(Exception e){ Product5 is consumed.
System.out.println(e); Product6 is produced.
} Product6 is consumed.
} Product7 is produced.
a=x; Product7 is consumed.
System.out.println("Product" + a + " is produced."); Product8 is produced.
produced = true; Product8 is consumed.
notify(); Product9 is produced.
} Product9 is consumed.
Product10 is produced.
public synchronized void consume(){ Product10 is consumed.
if(!produced){
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(){
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(){
for(int i = 1; i <= 10; i++){
b.consume();
}
}
}
public class intertrdcommProducerConsumerExample9 {
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();
}
}