Professional Documents
Culture Documents
Inter-Thread Communication in Java: Inter-Thread Communication in Java Is A Technique Through Which
Inter-Thread Communication in Java: Inter-Thread Communication in Java Is A Technique Through Which
wait() method in Java notifies the current thread to give up the monitor
(lock) and to go into sleep state until another thread wakes it up by
calling notify () method. This method throws InterruptedException.
Various forms of wait () method allow us to specify the amount of
time a thread can wait. They are as follows:
Syntax:
public final void wait()
The notify () method wakes up a single thread that called wait() method
on the same object. If more than one thread is waiting, this method will
awake one of them. The general syntax to call notify () method is as
follows:
Syntax:
public final void notify()
The notifyAll() method is used to wake up all threads that called wait()
method on the same object. The thread having the highest priority will
run first.
The general syntax to call notifyAll() method is as follows:
Syntax:
public final void notifyAll()
{
int i;
this.i = i;
return i;
A obj;
Thread1(A obj)
this.obj = obj;
{
for(int j = 1; j <= 5; j++){
obj.deliver(j);
A obj;
Thread2(A obj)
this.obj = obj;
obj.receive();
{
public static void main(String[] args)
t1.start();
t2.start();
Output:
Data Delivered: 1
Data Delivered: 2
Data Delivered: 3
Data Delivered: 4
Data Delivered: 5
Data Received: 5
Data Received: 5
Data Received: 5
Data Received: 5
Data Received: 5
Data Received: 5
Explanation:
In this example program, data delivered by Thread1 is used by
Thread2. Thread1 produces data 1 to 5 and delivered to Thread2 to
receive it but due to no communication between these two threads,
Thread2 uses data 5 five times in a row.
Let’s rewrite the above program where we will use wait() and notify()
method to establish the communication between two threads.
Program source code 2:
// In this program, we will understand how to use wait and notify.
public class A
int i;
boolean flag = false; // flag will be true when data production is over.
if(flag)
try
wait(); // Wait till a notification is received from Thread2. There will be no wastage
of time.
catch(InterruptedException ie)
System.out.println(ie);
}
this.i = i;
flag = true; // When data production is over, it will store true into flag.
notify(); // When data production is over, it will notify Thread2 to use it.
if(!flag)
try {
catch(InterruptedException ie){
System.out.println(ie);
flag = false; // It will store false into flag when data is received.
notify(); // When data received is over, it will notify Thread1 to produce next data.
return i;
}
public class Thread1 extends Thread
A obj;
Thread1(A obj)
this.obj = obj;
obj.deliver(j);
}}
A obj;
Thread2(A obj)
this.obj = obj;
obj.receive();
}}
// Creating two thread objects and pass reference variable obj as parameter to Thread1
and Thread2.
t1.start();
t2.start();
Output:
Data Delivered: 1
Data Received: 1
Data Delivered: 2
Data Received: 2
Data Delivered: 3
Data Received: 3
Data Delivered: 4
Data Received: 4
Data Delivered: 5
Data Received: 5
Explanation:
1. In this example program, wait() and notify() methods are called
inside deliver() and receive() method. Both methods enable Thread1 to
notify Thread2 after producing data and wait until Thread2 complete
using.
2. In the same way, Thread2 after using data notifies Thread1 and
waits until Thread1 produces and delivers the next data. Thus, the
output comes in a synchronized form.
3. If the flag is true, Thread2 takes data from Thread1 and use it. When
Thread1 is busy producing data, now and then Thread2 will check flag
is true or not.
If the flag shows false, then Thread2 will wait for the object until it
receives a notification from a notify() method.
When the data production is over, Thread1 will send a notification
immediately to Thread2 to receive data. In this way, Thread1 and
Thread2 communicate with each other efficiently.
Producer-Consumer Problem in Java using wait and notify methods
public class Q
int i;
if(valueSet)
try
wait();
}
catch(InterruptedException ie)
System.out.println(ie);
this.i = i;
valueSet = true;
notify();
if(!valueSet)
try {
wait();
catch(InterruptedException ie){
System.out.println(ie);
valueSet = false;
notify();
return I;
Q q;
Producer(Q q)
this.q = q;
q.produce(j);
Q q;
Consumer(Q q)
{
this.q = q;
q.consume();
Q q = new Q();
p.start();
c.start();
}
}
Output:
Data Produced: 1
Data Consumed: 1
Data Produced: 2
Data Consumed: 2
Data Produced: 3
Data Consumed: 3
Data Produced: 4
Data Consumed: 4
Data Produced: 5
Data Consumed: 5