标签:exec name with pre second 定时 system 执行 delay
通过JDK自带工具实现一个间隔并发执行的定时任务,代码如下:
public class Test{ public static void main(String[] args) { ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1); scheduledThreadPoolExecutor.scheduleWithFixedDelay(new Runnable() { @Override public void run() { System.out.print(LocalDateTime.now() + " "); System.out.println(Thread.currentThread().getName() + "= test scheduleWithFixedDelay......"); } }, 3, 2, TimeUnit.SECONDS);// 3秒后开始执行任务,每隔2秒执行任务 } }
执行结果如下:
2020-05-24T20:17:32.022 pool-1-thread-1= test scheduleWithFixedDelay......
2020-05-24T20:17:34.023 pool-1-thread-1= test scheduleWithFixedDelay......
2020-05-24T20:17:36.025 pool-1-thread-1= test scheduleWithFixedDelay......
2020-05-24T20:17:38.026 pool-1-thread-1= test scheduleWithFixedDelay......
2020-05-24T20:17:40.029 pool-1-thread-1= test scheduleWithFixedDelay......
2020-05-24T20:17:42.030 pool-1-thread-1= test scheduleWithFixedDelay......
2020-05-24T20:17:44.031 pool-1-thread-1= test scheduleWithFixedDelay......
经典笔试题:通过JDK自带工具实现一个间隔并发执行的定时任务
标签:exec name with pre second 定时 system 执行 delay
原文地址:https://www.cnblogs.com/gaopengpy/p/12952475.html