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 unit
delay參數的時間單位(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 unit
delay參數的時間單位(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
...