ScheduledThreadPoolExecutor
ScheduledThreadPoolExecutor实现了ScheduledExecutorService接口,
该接口定义了可延时执行异步任务和可周期执行异步任务的特有功能,相应的方法分别为:
1.达到给定的延时时间后,执行任务。这里传入的是实现Runnable接口的任务,因此通过ScheduledFuture. get()获取结果为null
public ScheduledFuture<?> schedule(Runnable command,
long delay,
TimeUnit unit);
2.达到给定的延时时间后,执行任务,这里传入的是实现Callable接口的任务,因此,返回的是任务的最终计算结果
publle <> ScheduledFuture<> schedule(Callable<> callable,
long delay,
TImeUnit unit);
3.是以上一个任务开始的时间计时, period时间过去后,检测上一个任务是否执行完毕, 如果上一个任务执行完毕,则当前任务立即执行,如果上一个任务没有执行完毕,则需要等上一个任务执行完毕后立即执行, 周期性执行任务。
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit);
4.当达到延时时间initalelay后,任务开始执行。上一个任务执行结束后到下一 次任务执行,中间延时时间间隔为delay. 以这种方式,周期性执行任务
public ScheduledFuture<?>scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);