// Class   : TimerUser
// Author  : Syam Gadde
// 
// Illustrates proper and improper use of the Timer class

public class TimerUser extends Thread implements TimerHandler
{
  int myTime;

  TimerUser(int duration) {
    Timer timer = new Timer(duration, this);
    timer.start();
  }

  public void timerStarted(Timer timer) {
    myTime = 30;
  }

  public void timerStopped(Timer timer) {
  }

  public void timerExpired(Timer timer) {
  }

  public void timerPulse(Timer timer) {
    int delta = timer.getElapsed() - (30 - myTime);

    // this version doesn't work correctly
    // fe.tick();
    //myTime--;
    //System.out.println(myTime + " seconds left.");

    // this version works more correctly
    for (int i=0; i<delta; i++) {
      // fe.tick();
      myTime--;
      System.out.println(myTime + " seconds left.");
    }
  }

  public void run() {
    int counter = 0;
    while (true) {
      counter++;
      if (counter%500000 == 0) {
	System.out.println("Yield!");
	yield();
      }
    }
  }

  public static void main (String [] args) {
    TimerUser tu = new TimerUser(30);
    Thread life = new Thread(tu);
    life.start();
  }
}

