IDEA 对JUC进行了一个简单封装,可以到util.jar中的com.intellij.util.concurrency包中查看详情,本文主要记录一下IDEA插件开发中如何使用定时任务。
插件中的定时任务主要使用AppExecutorUtil来实现:
从上图可以看出IDEA的定时任务就是使用JUC的ScheduledExecutorService来完成的。
另外,本文再补充一下scheduleAtFixedRate 和 scheduleWithFixedDelay区别:
- scheduleAtFixedRate:根据exeuteTime和period的长短不同,周期性执行时间点稍微有所不同,executeTime > period的时候,就是上一次任务执行完成马上开始执行下一次任务;当period > executeTime时,则是按照period计算,到点后开始执行任务。
- scheduleWithFixedDelay:这种方式周期执行的时间点比较好计算,以上一次任务结束时间点开始计时,推迟执行delay时间,进行下一次任务。
参见:ScheduledThreadPoolExecutor 中ScheduleAtFixedRate 和 ScheduleWithFixedDelay方法讲解