线程池之调度池newScheduledThreadPool

作者: 激情的狼王 | 来源:发表于2017-09-26 15:31 被阅读0次

new一个线程数为4的线程池:
ScheduledExecutorService service = Executors.newScheduledThreadPool(4);
官方API提供四个方法如图:


QQ图片20170926152137.png

即兴翻译:

1.schedule(Callable<V> callable, long delay, TimeUnit unit)
创建并执行并销毁一个callable在延迟指定delay时间后
2.同1,callable换成了runnable
3.scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
创建并执行并结束一个runnable在延迟指定initialDelay时间,然后,每隔initialDelay+period*n时间执行一次
4.scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
创建并执行并结束一个runnable在延迟指定initialDelay时间,然后第一次执行完成后,间隔delay时间继续执行一次,无限循环。

测试代码:

QQ图片20170926153212.png

难点:
1.scheduleAtFixedRate如果执行时间小于指定的间隔时间的情况下,callable或runnable每隔period执行一次,如果执行时间大于指定的间隔时间,每隔程序执行时间执行一次。
2.scheduleWithFixedDelay,不管执行时间怎么样,两次执行时间之间必须间隔delay时间。

相关文章

网友评论

    本文标题:线程池之调度池newScheduledThreadPool

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