美文网首页
ScheduledThreadPoolExecutor使用

ScheduledThreadPoolExecutor使用

作者: 王凡77 | 来源:发表于2020-07-03 18:59 被阅读0次

ScheduledThreadPoolExecutor 类继承自ThreadPoolExecutor,相关线程池的创建爱见ThreadPoolExecutor类分析,本文主要分析实现ScheduledExecutorService接口的四个方法

1、public ScheduledFuture schedule(Runnable command,long delay, TimeUnit unit);

延迟启动任务 delay标识延迟时间 unit为delay的单位。

2、public ScheduledFuture schedule(Callable callable,long delay, TimeUnit unit);

与1的区别是接收的是一个Callable对象,可以获取到执行的返回值

3、public ScheduledFuture scheduleAtFixedRate(Runnable command,long initialDelay,

long period,TimeUnit unit);

4、public ScheduledFuture scheduleWithFixedDelay(Runnable command,long initialDelay,

long delay,TimeUnit unit);

3与4均为延迟initialDelay以delay为周期执行任务。二者的区别在于scheduleAtFixedRate不会计算任务本身的时间。如果执行时间大于延迟时间会立刻执行。scheduleWithFixedDelay的实际执行周期为延迟时间delay+任务执行时间。

相关文章

网友评论

      本文标题:ScheduledThreadPoolExecutor使用

      本文链接:https://www.haomeiwen.com/subject/mblrqktx.html