Concurrent Programming with Java
Lab Manual, Version 1.0, F. Astha Ekadiyanto,
2002.
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 | |||||
Sleeping (Doing some other process) |
Producing (About to access Buffer) | Waiting (Trying to access Buffer) | |||
Producer | |||||
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: