(timer) java.util.TimerTask.stop()/.proceed() please

XMLWordPrintable

    • Type: Enhancement
    • Resolution: Won't Fix
    • Priority: P4
    • None
    • Affects Version/s: 1.4.1
    • Component/s: core-libs
    • x86
    • windows_nt

      Name: rmT116609 Date: 06/25/2003


      A DESCRIPTION OF THE REQUEST :
      I have the need to observe a thread with a TimerTask. After a period of time (timeout) the timer task should take action. However, it could happen, that the timer task should sleep for some time as the observed thread is doing something that should not be timed out. Therefore, i would like to tell the timer to stop counting and continue later when i tell it.

      JUSTIFICATION :
      Flexibal use of TimerTask

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      TimerTask.stop() should "pause", TimerTask.proceed() should continue running.
      ACTUAL -
      Once a TimerTask is started, there is no way to stop/halt/pause it.

      ---------- BEGIN SOURCE ----------
      ...
      Timer timer = new Timer();
      timer.schedule(new JobTimer(this, timeout), 0, 1000); //1000 = each second
      try {
          processJob(); //should be able to 'pause' timer
      } catch (Exception e) {
          System.out.println(""+e);
      } finally {
          timer.cancel();
      }
      ....



          class JobTimer extends TimerTask {
              
              /** Start time in milliseconds. */
              private long startTime = 0; //start value
              /** Number of milliseconds to wait till timeout occurs. */
              private long timeout = 0;
              
              
              /**
               * Constructor.
               *
               * @param jobThread Thread to observe.
               */
              public JobTimer(ExchangeThread jobThread, long timeout) {
                  startTime = System.currentTimeMillis();
                  this.timeout = timeout;
              }//JobTimer()
              
              /**
               * Overwrites super.
               */
              public void run() {
                  long time = (System.currentTimeMillis() - startTime);
      //System.out.println("timer: "+time);
                  if (time > timeout) { //timoute occured?
      System.out.println("Timeout after "+timeout+" milliseconds.");
                      //todo: what to do with input thread after timeout?

                     cancel(); //stop and quit timer
                  }
              }//run()
          
          }//JobTimer
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      implement your own stop()/proceed() methods in a subclass of TaskTimer and increase temporary timeout as long as stop() is called till proceed() is called:

          class TimeoutTimer extends TimerTask {
              
              /** Start time in milliseconds. */
              private long startTime = 0; //start value
              /** Number of milliseconds to wait till timeout occurs. */
              private long timeout = 0;
              /** Real timeout which is increased during stop() calls. */
              private long realTimeout = 0;
              /** Time when stop() was called. */
              private long stopTime = 0;
              
              
              /**
               * Constructor.
               *
               * @param jobThread Thread to observe.
               */
              public TimeoutTimer(Object jobThread, long timeout) {
                  startTime = System.currentTimeMillis();
                  this.timeout = timeout;
                  realTimeout = timeout;
              }//JobTimer()
              
              /**
               * Overwrites super.
               */
              public void run() {
                  long time = (System.currentTimeMillis() - startTime);
      //System.out.println("timer: "+time);
                  if (time > realTimeout) { //timoute occured?
      System.out.println("Timeout after "+timeout+" milliseconds.");
                      //todo: kill jobThread?
                     cancel(); //stop and quit timer
                  }
              }//run()
          
              /**
               * Stop timeout counting.
               */
              public void stop() {
                  stopTime = System.currentTimeMillis();
              }
              
              /**
               * Continue timeout counting.
               */
              public void proceed() {
                  realTimeout += System.currentTimeMillis() - stopTime;
              }
          
          }//TimeoutTimer
      (Review ID: 186379)
      ======================================================================
      ###@###.### 11/1/04 22:56 GMT

            Assignee:
            Stuart Marks
            Reporter:
            Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: