Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: changed step to time

...

Code Block
bgColor#FFcccc
class SleepyThread implements Runnable {
  private static final Object lock = new Object();
  private static int steptime = 1;
  private int myStep; // do stuff when the step raches this value

  public SleepyThread(int myStep) {
    this.myStep = myStep;
  }

  public void run () {
    try {
      synchronized (lock) {
        while (time != myStep) { 
          lock.wait();  
        }

        // ... do stuff

        time++;
        lock.notify();
      }
    } catch (InterruptedException ie) {
      Thread.currentThread().interrupt(); // Reset interrupted status
    }    
  }

  public static void main(String[] args) {
    for (int i = 5; i > 0; i--) {
      SleepyThread ms = new SleepyThread(i);
      new Thread(ms).start();
    }
  }
}

...