jdk schedule timer task

Implementing and scheduling a task to be executed by a timer
1) Implement a custom subclass of TimerTask. The run method contains the code that performs the task.
    class RemindTask extends TimerTask {
        public void run() {
            System.out.println("Time up!");
            System.exit(0);
        }
    }
2) Create a thread by instantiating the Timer class
    Timer timer = new timer();
3) Instantiate the timer task object(new RemindTask())
    RemindTask task = new RemindTask();
4) Schedule the timer task for execution.
  (1) execute the task after special milliseconds delay.
    timer.schedule(task,5*1000);
  (2) specify the time when the task suould execute.
    //execute the task at 11:01 p.m
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY,23);
    calendar.set(Calendar.MINUTE,1);
    calendar.set(Calendar.SECOND,0);
    Date time = calendar.getTime();

    timer.schedule(task,time);

Stopping Timer Threads
  By defaulst, aprogram keeps running as long as its timer threads are running. There is four ways to terminate a timer thread
  1) Invoke cancel on the timer.(timer.cancel())
  2) Make the timer's thread a daemon(后台), by creating the timer like this: new Timer(true). If the only threads left in the program are daemon threads, the program exits.
  3) After all the timer scheduleds tasks have finished executing,remove all references to the Timer object. the timer thread will terminate.
  4) Invoke the System.exit method, which makes the entire program and all its threads exit.

Performing a Task repeatedly
There is four Timer method to perform a task repeatedly
  * schedule(TimerTask task, long delay, long period)
    Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.
    执行重复的任务,第一次在延时时间后执行,往后的以特定的时间间隔执行
    timer.schedule(new RemindTask(),3*1000,1*1000)
    RemindTask任务将会在3秒后执行,以后将会以1秒的间隔重复执行

  * schedule(TimerTask task, Date time, long period)
    执行重复的任务,第一次在特定的时间执行,往后的以特定的时间间隔执行

  * scheduleAtFixedRate(TimerTask task, long delay, long period)
    Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals, separated by the specified period.
    以固定的延时执行重复的任务,首次执行在特定的延时之后,以后的执行发生在特定的时间间隔之后
    temer.scheduleAtFixedRate(new RemindTask(),3*1000,1*1000)
  * scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
    执行重复的任务,第一次在特定的时间执行,往后的以特定的时间间隔执行

  schedule和scheduleAtFixedRate的区别在于,schedule以固定的相对时间间隔执行,如果某一次执行被延时了,往后的执行 的执行时间也会相对延时;而scheduleAtFixedRate是以绝对的时间间隔执行,如果某一次执行被延时,它的后一次执行的延时将会缩短。

 /**
  * TimerTask第一次执行时间与被调度之间的间隔
  */
 public static final int DELAY = 1000 * 10;
 
 /**
  * TimerTask循环执行之间的时间间隔10s
  */
 public static final int PERIOD_BETWEEN_LOOP = 1000 * 10;

 Calendar calendar = GregorianCalendar.getInstance();
  calendar.set(Calendar.MINUTE, 50);
  calendar.set(Calendar.SECOND, 0);
  Date startTime = calendar.getTime();
  
  TimerTask task = new TimerTaskImpl();
  Timer timer = new Timer();
  
  //测试时需要逐一测试,否则会出错
  //java.lang.IllegalStateException: Task already scheduled or cancelled
  
  //执行一次,startTime在当前时间之前则马上执行
  //timer.schedule(task, startTime);
  //执行一次
  //timer.schedule(task, DELAY);
  //startTime时间在当前时间之后,
  //则从startTime开始循环执行,即使某个时间间隔中由于某种问题间隔时间>10S,下一次间隔时间也仍为10S
  //Sat Mar 19 22:50:00 CST 2011
  //Sat Mar 19 22:50:11 CST 2011
  //Sat Mar 19 22:50:21 CST 2011
  //startTime时间在当前时间之前,
  //则从当前时间开始循环执行N次
  //Sat Mar 19 22:57:53 CST 2011
  //Sat Mar 19 22:58:04 CST 2011
  //Sat Mar 19 22:58:14 CST 2011
  //timer.schedule(task, startTime, PERIOD_BETWEEN_LOOP);
  //从被调度的时间开始计时,DELAY之后第一次执行,之后每隔PERIOD_BETWEEN_LOOP执行一次
  //对时间间隔超时的处理方式与上一方法一致
  //timer.schedule(task, DELAY, PERIOD_BETWEEN_LOOP);
  //startTime时间在当前时间之后,
  //则从startTime开始循环执行,如果某个时间间隔中由于某种问题间隔时间>10S,下一次间隔时间则会<10s,从而使频率不变
  //startTime时间在当前时间之前,
  //则立即执行(currentTime-startTime) / PERIOD_BETWEEN_LOOP + 1次,然后以上述方式进行循环
  timer.scheduleAtFixedRate(task, startTime, PERIOD_BETWEEN_LOOP);
  //从被调度的时间开始计时,DELAY之后第一次执行,之后每隔PERIOD_BETWEEN_LOOP执行一次
  //对时间间隔超时的处理方式与上一方法一致
  //timer.scheduleAtFixedRate(task, DELAY, PERIOD_BETWEEN_LOOP);

猜你喜欢

转载自ssydxa219.iteye.com/blog/1750168