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

[Contents] [Next] [Previous]

Lab 5: Monitors and Thread Synchronization 1


Thread Monitor Demonstration: DemoThread Class

The DemoThread Class is the Thread that being controlled by the Applet. The codes itself is actually very straight forward. The most important properties of this Thread is its state and the monitor Object that is used by every DemoThread that are trying to access a common resource. You may want to observe the run() method and the runMonitor() method to find out how the DemoThread actually being managed by the Applet.

Create a DemoThread Class and replace with the code below:

/**
 * The class DemoThread implements a thread of the monitor demonstration.
 * @author Stephan Fischli
 * @version 1.0
 */
public class DemoThread extends Thread
{
   public static final int CREATED = 0;
   public static final int RUNNING = 1;
   public static final int ENTERING_MONITOR = 2;
   public static final int RUNNING_MONITOR = 3;
   public static final int LEAVING_MONITOR = 4;
   public static final int WAITING_CONDITION = 5;
   public static final int SIGNALING_CONDITION = 6;
   public static final int DEAD = 6;
  /**
   * The monitor used by the thread.
   */
   private Object monitor;
  /**
   * The state of the thread.
   */
   private volatile int state;
  /**
   * Creates a DemoThread object.
   * @param monitor the monitor used by the thread
   */
   public DemoThread( Object monitor )
   {
        this.monitor = monitor;
        state = CREATED;
   }
  /**
   * Runs the thread.
   */
   public void run()
   {
        state = RUNNING;
        while ( true ) {
             switch ( state ) {
                  case ENTERING_MONITOR:
                       synchronized ( monitor ) { runMonitor(); }
                       break;
                  case DEAD:
                       return;
             }
             state = RUNNING;
             try { sleep( 100 ); }
             catch ( InterruptedException e ) {}
        }
   }
  /**
   * Runs the thread in the monitor.
   */
   public void runMonitor()
   {
        state = RUNNING_MONITOR;
        while ( true ) {
             switch ( state ) {
                  case WAITING_CONDITION:
                       try { monitor.wait(); }
                       catch ( InterruptedException e ) {}
                       break;
                  case SIGNALING_CONDITION:
                       monitor.notifyAll();
                       break;
                  case LEAVING_MONITOR:
                       return;
             }
             state = RUNNING_MONITOR;
             try { sleep( 100 ); }
             catch ( InterruptedException e ) {}
        }
   }
  /**
   * Returns the state of the thread.
   */
   public int getState()
   {
        return state;
   }
  /**
   * Tells the thread to enter the monitor.
   */
   public void enterMonitor()
   {
        state = ENTERING_MONITOR;
   }
  /**
   * Tells the thread to leave the monitor.
   */
   public void leaveMonitor()
   {
        state = LEAVING_MONITOR;
   }
  /**
   * Tells the thread to wait for the condition.
   */
   public void waitCondition()
   {
        state = WAITING_CONDITION;
   }
  /**
   * Tells the thread to signal the condition.
   */
   public void signalCondition()
   {
        state = SIGNALING_CONDITION;
   }
 /**
   * Tells the thread to terminate.
   */
   public void terminate()
   {
        state = DEAD;
   }
}
 


[Contents] [Next] [Previous]