Two or more threads communicate with each other by exchanging the messages. This mechanism is called interthread communication
Inter Thread Communication
• Two or
more threads communicate with each other by exchanging the messages. This
mechanism is called interthread communication.
• Polling
is a mechanism generally implemented in a loop in which certain condition is
repeatedly checked.
• To
better understand the concept of polling, consider producer-consumer problem in
which producer thread produces and the consumer thread consumes whatever is
produced.
• Both
must work in co-ordination to avoid wastage of CPU cycles.
• But
there are situations in which the producer has to wait for the consumer to
finish consuming of data. Similarly the consumer may need to wait for the
producer to produce the data.
• In
Polling system either consumer will waste many CPU cycles when waiting for
producer to produce or the producer will waste CPU cycles when waiting for the
consumer to consume the data.
• In
order to avoid polling there are three in-built methods that take part in
inter-thread communication -
Example
Program
Following
is a simple Java program in which two threads are created one for producer and
another
is for consumer.
The
producer thread produces(writes) the numbers from 0 to 9 and the consumer
thread consumes(reads) these numbers.
The wait
and notify methods are used to send particular thread to sleep or to resume the
thread from sleep mode respectively.
Java Program[InterThread.java]
class
MyClass
{
int val;
boolean
flag = false;
synchronized
int get() //by toggling the flag synchronised read and write is performed
{
if(!flag)
try
{
wait();
}
catch
(InterruptedException e)
{
System.out.println("InterruptedException!!!");
}
System.out.println("Consumer
consuming: " + val);
flag =
false;
notify();
return
val;
}
synchronized
void put(int val)
{
if(flag)
try
{
wait();
}
catch
(InterruptedException e)
{
System.out.println("InterruptedException!!!");
}
this.val
= val;
flag = true;
System.out.println("Producer
producing " + val);
notify();
}
}
class
Producer extends Thread
{
MyClass
th1;
Producer(MyClass
t).
{
th1 = t;
}
public
void run()
{
for(int
i =0;i<10;i++)
{
th1.put(i);
}
}
}
Class Consumer
Extends Thread
{
MyClass
th2;
Consumer(MyClass
t)
{
th2 = t;
}
public
void run()
{
for(int
I = 0;i<10;i++)
{
th2.get();
}
}
}
class
InterThread
{
public static
void main(String[] arg)
{
MyClass
TObj = new MyClass();
Producer
pthread=new Producer(TObj);
Consumer
cthread=new Consumer(TObj);
pthread.start();
cthread.start();
}
}
Output
Producer
producing 0
Consumer
consuming: 0
Producer
producing 1
Consumer
consuming: 1
Producer
producing 2
Consumer
consuming: 2
Producer
producing 3
Consumer
consuming: 3
Producer
producing 4
Consumer
consuming: 4
Producer
producing 5
Consumer
consuming: 5
Producer
producing 6
Consumer
consuming: 6
Producer
producing 7
Consumer
consuming: 7
Producer
producing 8
Consumer
consuming: 8
Producer
producing 9
Consumer
consuming: 9
Program Explanation
• Inside get() -
The
wait() is called in order to suspend the execution by that time the producer
writes the value and when the data gets ready it notifies other thread that the
data is now ready. Similarly when the consumer reads the data execution inside
get() is suspended. After the data has been obtained, get() calls notify().
This tells producer that now the producer can write the next data in the queue.
• Inside
put() -
The
wait() suspends execution by that time the consumer removes the item from the
queue. When execution resumes, the next item of data is put in the queue, and
notify() is called. When the notify is issued the consumer can now remove the
corresponding item for reading.
Object Oriented Programming: Unit III: Exception Handling and Multithreading : Tag: : with Example Java Programs - Inter Thread Communication
Object Oriented Programming
CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation