Resettable Java Timer

前端 未结 8 1076
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 07:31

I\'d like to have a java.utils.Timer with a resettable time in java.I need to set a once off event to occur in X seconds. If nothing happens in between the time the timer wa

相关标签:
8条回答
  • 2020-11-28 07:54

    According to the Timer documentation, in Java 1.5 onwards, you should prefer the ScheduledThreadPoolExecutor instead. (You may like to create this executor using Executors.newSingleThreadScheduledExecutor() for ease of use; it creates something much like a Timer.)

    The cool thing is, when you schedule a task (by calling schedule()), it returns a ScheduledFuture object. You can use this to cancel the scheduled task. You're then free to submit a new task with a different triggering time.

    ETA: The Timer documentation linked to doesn't say anything about ScheduledThreadPoolExecutor, however the OpenJDK version had this to say:

    Java 5.0 introduced the java.util.concurrent package and one of the concurrency utilities therein is the ScheduledThreadPoolExecutor which is a thread pool for repeatedly executing tasks at a given rate or delay. It is effectively a more versatile replacement for the Timer/TimerTask combination, as it allows multiple service threads, accepts various time units, and doesn't require subclassing TimerTask (just implement Runnable). Configuring ScheduledThreadPoolExecutor with one thread makes it equivalent to Timer.

    0 讨论(0)
  • 2020-11-28 07:55

    Here is the example for Resetable Timer . Try to change it for your convinence...

    package com.tps.ProjectTasks.TimeThread;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;
    
    /**
     * Simple demo that uses java.util.Timer to schedule a task to execute
     * every 5 seconds and have a delay if you give any input in console.
     */
    
    public class DateThreadSheduler extends Thread {  
        Timer timer;
        BufferedReader br ;
        String data = null;
        Date dNow ;
        SimpleDateFormat ft;
    
        public DateThreadSheduler() {
    
            timer = new Timer();
            timer.schedule(new RemindTask(), 0, 5*1000); 
            br = new BufferedReader(new InputStreamReader(System.in));
            start();
        }
    
        public void run(){
    
            while(true){
                try {
                    data =br.readLine();
                    if(data != null && !data.trim().equals("") ){
                        timer.cancel();
                        timer = new Timer();
                        dNow = new Date( );
                        ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
                        System.out.println("Modified Current Date ------> " + ft.format(dNow));
                        timer.schedule(new RemindTask(), 5*1000 , 5*1000);
                    }
    
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public static void main(String args[]) {
            System.out.format("Printint the time and date was started...\n");
            new DateThreadSheduler();
        }
    }
    
    class RemindTask extends TimerTask {
        Date dNow ;
        SimpleDateFormat ft;
    
        public void run() {
    
            dNow = new Date();
            ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
            System.out.println("Current Date: " + ft.format(dNow));
        }
    }
    

    This example prints the current date and time for every 5 seconds...But if you give any input in console the timer will be delayed to perform the given input task...

    0 讨论(0)
  • 2020-11-28 08:02

    I made an own timer class for a similar purpose; feel free to use it:

    public class ReschedulableTimer extends Timer {
      private Runnable mTask;
      private TimerTask mTimerTask;
    
      public ReschedulableTimer(Runnable runnable) {
        this.mTask = runnable;
      }
    
      public void schedule(long delay) {
        if (mTimerTask != null)
          mTimerTask.cancel();
    
        mTimerTask = new TimerTask() {
          @Override
          public void run() {
            mTask.run();
          }
        };
        this.schedule(mTimerTask, delay);
      }
    }
    
    0 讨论(0)
  • 2020-11-28 08:04

    Do you need to schedule a recurring task? In that case I recommend you consider using Quartz.

    0 讨论(0)
  • 2020-11-28 08:10

    I don't think it's possible to do it with Timer/TimerTask, but depending on what exactly you want to achieve you might be happy with using java.util.concurrent.ScheduledThreadPoolExecutor.

    0 讨论(0)
  • 2020-11-28 08:11

    The whole Code snippet goes like this .... I hope it will be help full

    {
    
            Runnable r = new ScheduleTask();
            ReschedulableTimer rescheduleTimer = new ReschedulableTimer();
            rescheduleTimer.schedule(r, 10*1000);
    
    
        public class ScheduleTask implements Runnable {
            public void run() {
                //Do schecule task
    
            }
          }
    
    
    class ReschedulableTimer extends Timer {
            private Runnable task;
            private TimerTask timerTask;
    
            public void schedule(Runnable runnable, long delay) {
              task = runnable;
              timerTask = new TimerTask() { 
                  public void run() { 
                      task.run(); 
                      }
                  };
    
              timer.schedule(timerTask, delay);        
            }
    
            public void reschedule(long delay) {
                System.out.println("rescheduling after seconds "+delay);
              timerTask.cancel();
              timerTask = new TimerTask() { 
                  public void run() { 
                      task.run(); 
                  }
              };
              timer.schedule(timerTask, delay);        
            }
          }
    
    
    }
    
    0 讨论(0)
提交回复
热议问题