Concurrent Framework Timer

在1.5之后的java版本中使用ScheduledExecutorService替代了老旧的Timer.

ScheduledExecutorService的实现类是ScheduledThreadPoolExecutor, 一个具备Schedule task功能的ThreadPoolExecutor.

ScheduledExecutorService.submit(..) execute(..) invokeAll(..) 等方法与ExecutorService完全相同.

ScheduledExecutorService.schedule(Callable<T> callable, long delay, TimeUnit unit), 延迟delay*unit时间执行该线程.

ScheduledExecutorService.schedule(Runnable command, long delay, TimeUnit unit), 延迟delay*unit时间执行该线程.

ScheduledExecutorService.scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit), 多次执行该线程, 除非线程抛出Exception或cancelation/termination the executor, 不会并发执行, 延迟(initialDelay + period*(n-1))*unit时间多次执行第n次, 如果一个线程执行时间超过period*unit则在执行完该线程之后立刻开始下一个线程.

ScheduledExecutorService.scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)), 多次执行该线程, 除非线程抛出Exception或cancelation/termination the executor, 不会并发执行, 延迟initialDelay*unit时间执行第一个线程, 之后每隔delay*unit时间执行下一个线程.

原文地址:https://www.cnblogs.com/davidwang/p/3499738.html