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 Demonstration: ConsProdDemo Class (The Applet)

Now the Applet. The tasks in the Applet can be defined in the following sequences:

Create the ConsProdDemo Class and implement the codes as follow:

import java.applet.*;
import java.awt.*;
/**
 * The class ProducerConsumer demonstrates the producer consumer problem.
 * @author Stephan Fischli
 * @version 1.1
 */
public class ConsProdDemo extends Applet
{
   /**
   * The width of the applet.
   */
   private int width = 480;
  /**
   * The height of the applet.
   */
   private int height = 360;
  /**
   * The number of producers.
   */
   public int nprods = 3;
  /**
   * The number of consumers.
   */
   public int ncons = 3;
  /**
   * The capacity of the buffer.
   */
   public int bufcap = 5;
  /**
   * The producers.
   */
   private Producer[] producers;
  /**
   * The consumers.
   */
   private Consumer[] consumers;
  /**
   * The buffer.
   */
   private Buffer buffer;
  /**
   *The images of a producer.
   */
   private Image[] prodImages;
  /**
   * The images of a consumer.
   */
   private Image[] consImages;
  /**
   * The images of a table.
   */
   private Image[] tableImages;
  /**
   * Initializes the applet.
   */
   public void init()
   {
        // set background color
        // get parameters
        try {
             nprods = Integer.parseInt( getParameter( "nprods" ) );
             ncons = Integer.parseInt( getParameter( "ncons" ) );
             bufcap = Integer.parseInt( getParameter( "bufcap" ) );
             height = Math.max( 120*nprods, 120*ncons );
        }
        catch ( Exception e ) {}
        // load images
        prodImages = loadImages( "prod", 3 );
        consImages = loadImages( "cons", 3 );
        tableImages = loadImages( "table", 6 );
        setBackground( Color.white );
   }
  /**
   * Creates and starts the producers and consumers.
   */
   public void start()
   {
        // create buffer
        buffer = new Buffer( bufcap );
        // create and start producers
        producers = new Producer[nprods];
        for ( int i = 0; i < nprods; i++ ) {
             producers[i] = new Producer( buffer );
             producers[i].start();
        }
        // create and start consumers
        consumers = new Consumer[ncons];
        for ( int i = 0; i < ncons; i++ ) {
             consumers[i] = new Consumer( buffer );
             consumers[i].start();
        }
   }
  /**
   * Updates the applet.
   */
   public void update( Graphics g )
   {
        paint( g );
   }
  /**
   * Paints the applet.
   */
   public void paint( Graphics g )
   {
        // draw producers
        for ( int i = 0; i < nprods; i++ ) {
             Image image = null;
             switch ( producers[i].state() ) {
                  case Producer.PAUSING: image = prodImages[0]; break;
                  case Producer.PRODUCING: image = prodImages[1]; break;
                  case Producer.WRITING: image = prodImages[2]; break;
             }
             g.drawImage( image, 0, height/2 - 60*nprods + 120*i, this );
        }
        // draw consumers
        for ( int i = 0; i < ncons; i++ ) {
             Image image = null;
             switch ( consumers[i].state() ) {
                  case Consumer.PAUSING: image = consImages[0]; break;
                  case Consumer.READING: image = consImages[1]; break;
                  case Consumer.CONSUMING: image = consImages[2]; break;
             }
             g.drawImage( image, 360, height/2 - 60*ncons + 120*i, this );
        }
        // draw table
        Image image = tableImages[Math.min( buffer.size(), 5 )];
        g.drawImage( image, 120, height/2 - 60, this );
        // repaint applet
        try { Thread.sleep( 100 ); }
             catch ( InterruptedException e ) {}
        repaint();
   }
  /**
   * Loads an array of images.
   * @param name the name of the images
   * @param num the number of images
   */
   private Image[] loadImages( String name, int num )
   {
        Image[] images = new Image[num];
        for ( int i = 0; i < num; i++ ) {
             String filename = "img/" + name + i + ".gif";
             images[i] = getImage( getDocumentBase(), filename );
        }
        return images;
   }
}

Finally, your BlueJ ConsProd Project would look like the following figure. Try to execute the Applet and observe the results.
You could actually provide some parameters to the applet (namely: nprods, ncons, and bufcap).


[Contents] [Next] [Previous]