Concurrent Programming with Java
Lab Manual, Version 1.0, F. Astha Ekadiyanto, 2002.

[Contents] [Next] [Previous]

Lab 5: Monitors and Thread Synchronization 1


Consumer-Producer synchronization scenario Demonstration

All the codes and materials in this section are taken from http://www.hta-be.bfh.ch/~fischli/kurse/threads/prodcons/index.html

The figure above describes the Consumer-Producer scenario. Each person in the figure represents a single thread either as a Producer (green color) or as a Consumer (blue color).

The roles are:

Thus, the Producer and Consumer must have three consecutive states which are:

Sleeping
(Doing some other process)
Waiting (Trying to access Buffer) Consuming (After Accessing Buffer)
Consumer

cons0.gif

cons2.gif


cons1.gif

Sleeping
(Doing some other process)
Producing (About to access Buffer) Waiting (Trying to access Buffer)
Producer

prod0.gif

prod1.gif

prod2.gif
Cartoons by Mackie G. Majcher

Now let us implement the codes. First of all, create a new BlueJ Project called ConsProd. Then inside the ConsProd directory create another directory called img (this will be the place to store all the animation images). Get the images zip file (consprod.zip) and extract the in the Path to ConsProd\img\ directory. Then, create the following classes:


[Contents] [Next] [Previous]