Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Multi threading PRODUCER CONSUMER A Brief Introduction THE PROBLEM ❖In computing, the producer-consumer problem (also known as the bounded- buffer problem) is a classic example of a...

2 answer below »
Multi threading
PRODUCER CONSUMER
A Brief Introduction
THE PROBLEM
❖In computing, the producer-consumer
problem (also known as the bounded-
uffer problem) is a classic example of a
multi-process synchronization problem.
❖The problem describes two processes,
the producer and the consumer, which
share a common, fixed-size buffer used as
a queue.
A COMMON PROBLEM !
THE PROBLEM
❖The producer’s
job is to generate
data, put it into
the buffer, and
start again.
THE PROBLEM
❖At the same time,
the consumer is
consuming the data
(i.e. removing it from
the buffer), one
piece at a time.
THE PROBLEM
❖To make sure that
the producer won’t
try to add data into
the buffer if it’s full
and that the
consumer won’t try
to remove data from
an empty buffer.
ONE SOLUTION
❖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
uffer again.
ONE SOLUTION
❖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.
❖An inadequate solution
could result in a deadlock
where both processes are
waiting to be awakened.
YOU CAN UDO THIS
Using Synchronized notified
and wait
We will do it the easy way!
We will use a Blocking
Queue
Create three classes:
Main
◦ Creates threads Producers and Consumers
Produce
◦ Runnable Class that add random items (integers) to the
Blocking Queue
Consume
◦ Runnable Class that remove items from the Blocking Queue
LAB OVERVIEW
Implements runnable
Has a BlockingQueue queue reference as an instance variable
Has a Name
Has A count (number of items to produce)
These should be passed in via the constructo
Ove
ides run :
◦ Generates a random number 0-500
◦ Adds is it to the queue
◦ Prints Its name , the number it produced and the size of the Queue
◦ Sleeps for 100 milliseconds
◦ Loops count time
◦ When it is done print the name plus “exiting”
PRODUCER
Implements runnable
Has a BlockingQueue queue reference as an instance variable
Has a Name
These should be passed in via the constructo
Ove
ides run :
◦ Takes item from the queue
◦ Prints Its name , the number it consumed and the size of the Queue
◦ Sleeps for 100 milliseconds
◦ Loops indefinitely
PRODUCER
Create a blocking queue:
BlockingQueue queue = new LinkedBlockingQueue
(5);Has a Name
Create and start 5 threads with 5 producer objects – use the above queue – pass
5 in for the count
Create and start 5 threads with 5 consumer objects – use the above queue
The consumer threads don’t exit – create a way for them to end when there is
not more production
If only we had a thread safe way to count things….
MAIN METHOD
Answered 8 days After Nov 12, 2022

Solution

Amar Kumar answered on Nov 16 2022
48 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here