jdk自带定时器使用方法详解
首先看一下jdk自带定时器:
一种工具,线程用其安排以后在后台线程中执行的任务。可安排任务执行一次,或者定期重复执行。与每个Timer对象相对应的是单个后台线程,用于顺序地执行所有计时器任务。计时器任务应该迅速完成。如果完成某个计时器任务的时间太长,那么它会“独占”计时器的任务执行线程。因此,这就可能延迟后续任务的执行,而这些任务就可能“堆在一起”,并且在上述不友好的任务最终完成时才能够被快速连续地执行。
schedule(TimerTasktask,longdelay)安排在指定延迟后执行指定的任务。
schedule(TimerTasktask,Datetime)安排在指定的时间执行指定的任务。如果此时间已过去,则安排立即执行该任务。
schedule(TimerTasktask,longdelay,longperiod)安排指定的任务从指定的延迟后开始进行重复的固定延迟执行。如果由于任何原因(如垃圾回收或其他后台活动)而延迟了某次执行,则后续执行也将被延迟
schedule(TimerTasktask,DatefirstTime,longperiod)安排指定的任务在指定的时间开始进行重复的固定延迟执行。如果由于任何原因(如垃圾回收或其他后台活动)而延迟了某次执行,则后续执行也将被延迟。
packagetest;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.Timer;
importjava.util.TimerTask;
/**
*jdk自带定时器
*
*@authorLIUTIE
*
*/
publicclassJDKTimer{
publicstaticvoidmain(String[]args)throwsParseException{
//日期格式工具
finalSimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
Timertimer=newTimer();
//10s后执行定时器,仅执行一次
System.out.print(sdf.format(newDate()));
System.out.println("thetimeronewillbeexecutedafter10seconds...");
longmilliseconds=10*1000;
timer.schedule(newTimerTask(){
@Override
publicvoidrun(){
System.out.print(sdf.format(newDate()));
System.out.println("thetimeronehasfinishedexecution");
}
},milliseconds);
//12秒后执行定时器,每1s执行一次
System.out.print(sdf.format(newDate()));
System.out.println("thetimertwowillbeexecutedafter12seconds...");
//启动后延迟时间
longafterSs=12*1000;
//执行周期
longintervalSs1=1*1000;
timer.schedule(newTimerTask(){
//执行计数器
inti=0;
@Override
publicvoidrun(){
System.out.print(sdf.format(newDate()));
System.out.println("thetimertwohasexecution"+(++i)+"timers");
//执行10次后关闭定时器
if(i==10){
this.cancel();
}
}
},afterSs,intervalSs1);
//指定时间执行定时器,仅执行一次
System.out.print(sdf.format(newDate()));
System.out.println("thetimerthreewillbeexecutedat2017-06-2721:47:00...");
Datedate=sdf.parse("2017-06-2721:47:00");
timer.schedule(newTimerTask(){
@Override
publicvoidrun(){
System.out.print(sdf.format(newDate()));
System.out.println("thetimerthreehasfinishedexecution");
}
},date);
//从指定时间开始周期性执行
System.out.print(sdf.format(newDate()));
System.out.println("thetimerfourwillbeexecutedat2017-06-2721:48:00...");
//执行间隔周期
longintervalSs=1*1000;
//开始执行时间
DatebeginTime=sdf.parse("2017-06-2721:48:00");
timer.schedule(newTimerTask(){
//执行计数器
inti=0;
@Override
publicvoidrun(){
System.out.print(sdf.format(newDate()));
System.out.println("thetimerfourhasexecution"+(++i)+"timers");
//执行10次后关闭定时器
if(i==10){
this.cancel();
}
}
},beginTime,intervalSs);
}
}
执行结果
2017-06-2721:46:24thetimeronewillbeexecutedafter10seconds... 2017-06-2721:46:24thetimertwowillbeexecutedafter12seconds... 2017-06-2721:46:24thetimerthreewillbeexecutedat2017-06-2721:47:00... 2017-06-2721:46:24thetimerfourwillbeexecutedat2017-06-2721:48:00... 2017-06-2721:46:34thetimeronehasfinishedexecution 2017-06-2721:46:36thetimertwohasexecution1timers 2017-06-2721:46:37thetimertwohasexecution2timers 2017-06-2721:46:38thetimertwohasexecution3timers 2017-06-2721:46:39thetimertwohasexecution4timers 2017-06-2721:46:40thetimertwohasexecution5timers 2017-06-2721:46:41thetimertwohasexecution6timers 2017-06-2721:46:42thetimertwohasexecution7timers 2017-06-2721:46:43thetimertwohasexecution8timers 2017-06-2721:46:44thetimertwohasexecution9timers 2017-06-2721:46:45thetimertwohasexecution10timers 2017-06-2721:47:00thetimerthreehasfinishedexecution 2017-06-2721:48:00thetimerfourhasexecution1timers 2017-06-2721:48:01thetimerfourhasexecution2timers 2017-06-2721:48:02thetimerfourhasexecution3timers 2017-06-2721:48:03thetimerfourhasexecution4timers 2017-06-2721:48:04thetimerfourhasexecution5timers 2017-06-2721:48:05thetimerfourhasexecution6timers 2017-06-2721:48:06thetimerfourhasexecution7timers 2017-06-2721:48:07thetimerfourhasexecution8timers 2017-06-2721:48:08thetimerfourhasexecution9timers 2017-06-2721:48:09thetimerfourhasexecution10timers
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。