时间:2021-05-19
首先看一下jdk自带定时器:
一种工具,线程用其安排以后在后台线程中执行的任务。可安排任务执行一次,或者定期重复执行。与每个 Timer 对象相对应的是单个后台线程,用于顺序地执行所有计时器任务。计时器任务应该迅速完成。如果完成某个计时器任务的时间太长,那么它会“独占”计时器的任务执行线程。因此,这就可能延迟后续任务的执行,而这些任务就可能“堆在一起”,并且在上述不友好的任务最终完成时才能够被快速连续地执行。
schedule(TimerTask task,long delay) 安排在指定延迟后执行指定的任务。
schedule(TimerTask task,Date time) 安排在指定的时间执行指定的任务。如果此时间已过去,则安排立即执行该任务。
schedule(TimerTask task, long delay, long period) 安排指定的任务从指定的延迟后开始进行重复的固定延迟执行。如果由于任何原因(如垃圾回收或其他后台活动)而延迟了某次执行,则后续执行也将被延迟
schedule(TimerTask task,Date firstTime,long period) 安排指定的任务在指定的时间开始进行重复的固定延迟执行。如果由于任何原因(如垃圾回收或其他后台活动)而延迟了某次执行,则后续执行也将被延迟。
执行结果
2017-06-27 21:46:24the timer one will be executed after 10 seconds...2017-06-27 21:46:24the timer two will be executed after 12 seconds...2017-06-27 21:46:24the timer three will be executed at 2017-06-27 21:47:00...2017-06-27 21:46:24the timer four will be executed at 2017-06-27 21:48:00...2017-06-27 21:46:34the timer one has finished execution2017-06-27 21:46:36the timer two has execution 1 timers2017-06-27 21:46:37the timer two has execution 2 timers2017-06-27 21:46:38the timer two has execution 3 timers2017-06-27 21:46:39the timer two has execution 4 timers2017-06-27 21:46:40the timer two has execution 5 timers2017-06-27 21:46:41the timer two has execution 6 timers2017-06-27 21:46:42the timer two has execution 7 timers2017-06-27 21:46:43the timer two has execution 8 timers2017-06-27 21:46:44the timer two has execution 9 timers2017-06-27 21:46:45the timer two has execution 10 timers2017-06-27 21:47:00the timer three has finished execution2017-06-27 21:48:00the timer four has execution 1 timers2017-06-27 21:48:01the timer four has execution 2 timers2017-06-27 21:48:02the timer four has execution 3 timers2017-06-27 21:48:03the timer four has execution 4 timers2017-06-27 21:48:04the timer four has execution 5 timers2017-06-27 21:48:05the timer four has execution 6 timers2017-06-27 21:48:06the timer four has execution 7 timers2017-06-27 21:48:07the timer four has execution 8 timers2017-06-27 21:48:08the timer four has execution 9 timers2017-06-27 21:48:09the timer four has execution 10 timers以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
SpringBoot使用定时器使用方法添加@Scheduled注解设计cron参数即可packagecom.clsystem.Comm;importorg.sp
原生的Java定时器使用Java.util包下的定时器也很简单,具体代码如下://设置定时器开始时间Datetime=sdf.parse("2020-10-01
1,只执行一次的定时器复制代码代码如下://定时器异步运行functionhello(){alert("hello");}//使用方法名字执行方法vart1=w
java.util.concurrent.ScheduledThreadPoolExecutor是JDK1.6之后自带的包,功能强大,能实现定时器和延时加载的功
最近要用到定时任务,就是超过48小时的数据给关闭,前台就不显示了。还是头一次使用java的定时器,。。java定时器使用Timer类。有时间得看下源码了,看看具