inter thread communication
TRANSCRIPT
SOLVING PRODUCER/CONSUMER PROBLEM
USING INTER-THREAD COMMUNICATION
Presented by:
A P S SubashB.Tech, II Year.DEPT. OF COMPUTER SCIENCE AND ENGG.SWARNANDHRA COLLEGE OF ENGG&TECHNARSAPUR, ANDHRA PRADESH
SUBASH ANDEY
Contents What is producer/consumer problem ?
Solution for producer/consumer problem.
What is inter-thread communication ?
What are wait(), notify() and notifyAll()
Working of Interthread Communication
Applications
Conclusion
SUBASH ANDEY
What is producer/consumer problem ?
In computing , the producer–consumer problem is a classic example of a multi-process synchronization problem.
Here, the problem describes two threads: one is the producer and other one is the consumer. These two threads share a common, fixed-size buffer used as a queue.
The producer's job is to generate data, put it into the buffer, and start again. At the same time, the consumer is consuming the data (i.e., removing it from the buffer), one piece at a time.
The problem is to make sure that - The producer won't try to add data into the buffer if it's full.
- And the consumer won't try to remove data from an empty buffer.
Pictorial representation of producer/consumer problem
SUBASH ANDEY
What is the solution ? The solution for the producer is to either go to sleep or discard
data if the buffer is full.
The next time the consumer removes an item from the buffer, it notifies the producer, who starts to fill the buffer again.
In the same way, the consumer can go to sleep if it finds the buffer to be empty.
The next time the producer puts data into the buffer, it wakes up the sleeping consumer.
The solution can be reached by means of ”Inter-thread communication”.
An inadequate solution could result in a deadlock where both threads are waiting to be awakened.
SUBASH ANDEY
What is inter-thread communication ?
Inter-thread Communication is a mechanism by which two or more threads communicate smoothly with each other by way of sending and receiving messages among them.
Inter-thread Communication is an elegant mechanism which will be used to solve producer/consumer problem efficiently through the use of the following methods of Object class :
(1) wait() (2) notify()
(3) notifyAll()
SUBASH ANDEY
What are wait(), notify(), notifyAll() methods ? wait() tells the calling thread to give up the
monitor and go to sleep until some other thread enters the same monitor and calls notify() or notifyAll().
notify() wakes up a thread that called wait()
on the same object. notifyAll() wakes up all the threads that
called wait() on the same object. One of the threads will be granted access.
SUBASH ANDEY
Working of Inter-thread Communication
SUBASH ANDEY
Applications Producer/Consumer model is used in parallel
computing. Producer/Consumer model is key model for
threads programming, especially for the Pool of Threads model.
It is used for synchronization of concurrently running processes.
It is used in real-time critical applications with multi-Tasking.
SUBASH ANDEY
Program output without using ITC
SUBASH ANDEY
Program output using ITC
SUBASH ANDEY
Conclusion This presentation proved programmatically
that the producer/consumer problem can be effectively solved with the help of Inter-thread Communication. Inter-thread Communication successfully solves the problem by ensuring that:
- the producer won't try to add data into the buffer if it's full. - and the consumer won't try to remove data from an empty buffer.
SUBASH ANDEY
SUBASH ANDEY