juc——ScheduledExecutorService、Sc
2019-02-19 本文已影响10人
大黑跟小白的日常
ScheduledExecutorService
ScheduledThreadPoolExecutor
是什么?
可以用线程池来执行定时事件的一个封装好的工具类。
什么用?
比如我们每隔固定时间,将数据库符合条件的业务数据做一次运算操作(形成折线图),或清洗,或数据抽取....
这时就可以,使用多线程来定时触发事件,处理这一批数据。
如何用?
简单使用,如下图所示
![](https://img.haomeiwen.com/i15253776/de32da34eb565e8f.png)
![](https://img.haomeiwen.com/i15253776/73c0fc3124001a5c.png)
为什么用?
使用简单方便,在一些job框架源码中,也用了该类。提供了系列相佐的工具,便于搭配使用。提高开发效率。
推荐使用ScheduledThreadPoolExecutor。线程池ThreadPoolExecutor更为好用,全面。