Executors 之 newSingleThreadScheduledExecutor 构建定时任务/延时任务
schedule
延时任务,只执行一次
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit);
Runnable command(the task to execute)long delay延迟执行时间(the time from now to delay execution)TimeUnit unitdelay参数的时间单位(the time unit of the delay parameter)
scheduleAtFixedRate
延时任务,并循环执行
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);
Runnable command(the task to execute)long initialDelay执行第一次的延迟时间(the time to delay first execution)long period连续执行的间隔时间(the period between successive executions)TimeUnit unit前两个参数的时间单位(the time unit of the initialDelay and period parameters)
scheduleWithFixedDelay
延时任务,并循环执行
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit);
Runnable command(the task to execute)long initialDelay执行第一次的延迟时间(the time to delay first execution)long delay上一个执行器的终止与下一个执行器的开始之间的延迟(the delay between the termination of one execution and the commencement of the next)TimeUnit unitdelay参数的时间单位(the time unit of the delay parameter)
talk is cheap, show me the code.
System.out.format("[%s] - [%s] - main\n",
DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").format(LocalDateTime.now()),
Thread.currentThread().getName());
// 创建一个单线程的任务调度池
ScheduledExecutorService singleThreadScheduledPool = Executors.newSingleThreadScheduledExecutor();
// 延时5秒后执行,只执行一次
singleThreadScheduledPool.schedule(() -> {
System.out.format("[%s] - [%s] - schedule\n",
DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").format(LocalDateTime.now()),
Thread.currentThread().getName());
}, 5, TimeUnit.SECONDS);
// 延时一秒后执行第一次,再循环执行(每隔5秒执行一次)
singleThreadScheduledPool.scheduleAtFixedRate(() -> {
System.out.format("[%s] - [%s] - scheduleAtFixedRate\n",
DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").format(LocalDateTime.now()),
Thread.currentThread().getName());
}, 1, 5, TimeUnit.SECONDS);
// 延时一秒后执行第一次,再循环执行(上一次执行结束延时5秒后再执行下次任务)
singleThreadScheduledPool.scheduleWithFixedDelay(() -> {
System.out.format("[%s] - [%s] - scheduleWithFixedDelay\n",
DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").format(LocalDateTime.now()),
Thread.currentThread().getName());
}, 1, 5, TimeUnit.SECONDS);
控制台输出
[2020/05/28 10:53:09] - [main] - main
[2020/05/28 10:53:10] - [pool-1-thread-1] - scheduleAtFixedRate
[2020/05/28 10:53:10] - [pool-1-thread-1] - scheduleWithFixedDelay
[2020/05/28 10:53:14] - [pool-1-thread-1] - schedule
[2020/05/28 10:53:15] - [pool-1-thread-1] - scheduleAtFixedRate
[2020/05/28 10:53:15] - [pool-1-thread-1] - scheduleWithFixedDelay
[2020/05/28 10:53:20] - [pool-1-thread-1] - scheduleAtFixedRate
[2020/05/28 10:53:20] - [pool-1-thread-1] - scheduleWithFixedDelay
[2020/05/28 10:53:25] - [pool-1-thread-1] - scheduleAtFixedRate
[2020/05/28 10:53:25] - [pool-1-thread-1] - scheduleWithFixedDelay
[2020/05/28 10:53:30] - [pool-1-thread-1] - scheduleAtFixedRate
[2020/05/28 10:53:30] - [pool-1-thread-1] - scheduleWithFixedDelay
[2020/05/28 10:53:35] - [pool-1-thread-1] - scheduleAtFixedRate
[2020/05/28 10:53:35] - [pool-1-thread-1] - scheduleWithFixedDelay
[2020/05/28 10:53:40] - [pool-1-thread-1] - scheduleAtFixedRate
[2020/05/28 10:53:40] - [pool-1-thread-1] - scheduleWithFixedDelay
[2020/05/28 10:53:45] - [pool-1-thread-1] - scheduleAtFixedRate
[2020/05/28 10:53:45] - [pool-1-thread-1] - scheduleWithFixedDelay
...
本文深入探讨了Java中使用Executors创建单线程定时任务的方法,包括延时任务、固定频率和固定延迟的循环任务,通过代码示例展示了schedule、scheduleAtFixedRate及scheduleWithFixedDelay的用法。
7979

被折叠的 条评论
为什么被折叠?



