脚本宝典收集整理的这篇文章主要介绍了(二十)java多线程之ScheduledThreadPoolExecutor,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
本人邮箱: <kco1989@QQ.COM>
欢迎转载,转载请注明网址 http://blog.csdn.net/tianshi_kco
gIThub: https://github.com/kco1989/kco
代码已经全部托管github有需要的同学自行下载
引言
java 提供的线程池还有一个,那就是任务调度线程池ScheduledThreadPoolExecutor
,它其实是ThreadPoolExecutor
的一个子类.
理论
我们通过查看ScheduledThreadPoolExecutor
的源代码,可以发现ScheduledThreadPoolExecutor
的构造器都是调用父类的构造器,只是它使用的工作队列是java.util.concurrent.ScheduledThreadPoolExecutor.DelayedWorkQueue
通过名字我们都可以猜到这个是一个延时工作队列.
因为ScheduledThreadPoolExecutor
的最大线程是Integer.MAX_VALUE,而且根据源码可以看到execute
和submit
其实都是调用schedule
这个方法,而且延时时间都是指定为0,所以调用execute
和submit
的任务都直接被执行.
例子 搞几个延时炸弹
我们搞几个延时炸弹,让它们每个5s炸一次
public class TestMain { public static void main(String[] args) throws InterruptedException { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ScheduledThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(5); for (int i = 0; i < 5; i ++){ final int temp = i + 1; pool.schedule(() -> { System.out.println("第"+temp+"个炸弹爆炸时间:" + simpleDateFormat.format(new Date())); }, temp * 5, TimeUnit.SECONDS); } pool.shutdown(); System.out.println("end main时间:" + simpleDateFormat.format(new Date())); } }
运行结果:
end main时间:2016-11-03 19:58:31
第1个炸弹爆炸时间:2016-11-03 19:58:36
第2个炸弹爆炸时间:2016-11-03 19:58:41
第3个炸弹爆炸时间:2016-11-03 19:58:46
第4个炸弹爆炸时间:2016-11-03 19:58:51
第5个炸弹爆炸时间:2016-11-03 19:58:56
ok,这个类相对比较简单,我就不多讲了
后记
在正在项目中,一般如果需要使用定时任务,不会直接使用这个类的.有一个quartz
已经把定时任务封装的很好了.它是通过cron
表示时,可以指定某一个任务每天执行,或者每周三下午5点执行.更多的资料可以去查百度.或者等以后有机会我再整理一写常用jar用法系列文章.就这样了.
打赏
如果觉得我的文章写的还过得去的话,有钱就捧个钱场,没钱给我捧个人场(帮我点赞或推荐一下)
以上是脚本宝典为你收集整理的(二十)java多线程之ScheduledThreadPoolExecutor全部内容,希望文章能够帮你解决(二十)java多线程之ScheduledThreadPoolExecutor所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。