   import javax.realtime.*;

   public class LaunchClock extends RealtimeThread 
   {
      AbsoluteTime startTime;
      RelativeTime remainingTime;
      RelativeTime tick;
      Clock myClock;
      boolean counting;
      boolean go;
   
      public LaunchClock(AbsoluteTime at, RelativeTime countDown)
      {
         super();
         System.out.println("Launch clock constructor " ); 
         startTime = at;
         remainingTime = countDown;
         myClock = Clock.getRealtimeClock();
         counting = true;
         go = false;
         tick = new RelativeTime(1000,0);
      }
   
      public synchronized void launch() throws Exception
      { 
         System.out.println("waiting launch " ); 
         
           while(!go)
           { 
              System.out.println("executing wait" ); 
              try
              {
                 wait();
                 System.out.println("launch woken" ); 
              }
               catch(Exception ie)
               {
                  throw new Exception("Launch failed");
               }
           }
      }
      
      public RelativeTime getResolution()
      {
         return tick;
      }
   
      public synchronized AbsoluteTime getCurrentLaunchTime()
      {
         if(myClock.getTime().compareTo(startTime) < 0) 
           return new AbsoluteTime(startTime.add(remainingTime));
         else
           return new AbsoluteTime(myClock.getTime().add(remainingTime));
      }
   
     
      public synchronized void stopCountDown()
      {
          counting = false;
         	notifyAll();
      }
   
      public synchronized void restartCountDown()
      {
            counting = true;
            notifyAll();
      }
   
      public synchronized void resetCountDown(RelativeTime to)
      {
            remainingTime = to;
      }
   
      public void run()
      {  
        System.out.println("Clock waiting to tick " );   
         synchronized(this)
         {
            try
            {
               while(myClock.getTime().compareTo(startTime) < 0)
                  HighResolutionTime.waitForObject(this,startTime);
            
              System.out.println("clock ticking " ); 
              while(remainingTime.getMilliseconds() > 0)
              {
                  while(!counting) 
                  {
                    System.out.println("not counting" );
                    wait();
                  }
                  // wait(1000);
                  HighResolutionTime.waitForObject(this, tick);
                  System.out.println("tick" );
                  remainingTime.set(remainingTime.getMilliseconds() - 
                                     tick.getMilliseconds()); 
               }
             } catch(Exception ie) {
                  System.out.println("exception in run" +ie); }
            go = true; 
            System.out.println("Clock: Go, go, go " ); 
            this.notifyAll();
            //notify();
         }
         System.out.println("run method ending " ); 
      }

   }
