线程池(4)Executors.newScheduledThreadPool-只执行1次
2024-08-30 02:23:14
例子1:延迟3秒后,只执行1次
ScheduledExecutorService es = Executors.newScheduledThreadPool(5);
log.info("开始时间");
try {
for (int i = 0; i < 20; i++) {
Runnable syncRunnable = new Runnable() {
@Override
public void run() {
log.info(Thread.currentThread().getName());
}
};
es.schedule(syncRunnable, 3000, TimeUnit.MILLISECONDS);
}
} finally {
es.shutdown();
}
运行结果:
10:41:39.589 开始时间
10:41:42.595 pool-1-thread-1
10:41:42.595 pool-1-thread-2
10:41:42.595 pool-1-thread-3
10:41:42.595 pool-1-thread-4
10:41:42.596 pool-1-thread-2
10:41:42.596 pool-1-thread-5
10:41:42.596 pool-1-thread-1
10:41:42.596 pool-1-thread-3
10:41:42.597 pool-1-thread-4
10:41:42.597 pool-1-thread-2
10:41:42.597 pool-1-thread-5
10:41:42.597 pool-1-thread-1
10:41:42.598 pool-1-thread-3
10:41:42.598 pool-1-thread-4
10:41:42.598 pool-1-thread-2
10:41:42.599 pool-1-thread-5
10:41:42.599 pool-1-thread-1
10:41:42.600 pool-1-thread-3
10:41:42.600 pool-1-thread-4
10:41:42.600 pool-1-thread-2
调用的ThreadPoolExecutor:
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
corePoolSize=5,maximumPoolSize=Integer.MAX_VALUE
keepAliveTime=0纳秒
allowCoreThreadTimeout=false(默认)
采用延迟队列DelayedWorkQueue
因此,
- 线程池中的线程数永远是5,永久存活
- 对于新任务,当队列未满时,插入队列;当队列已满时,默认执行AbortPolicy,即抛出异常。
- 支持线程reuse
因此,类似newFixedThreadPool
public ScheduledThreadPoolExecutor(int corePoolSize) { super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, new DelayedWorkQueue()); }
最新文章
- iOS 原生地图地理编码与反地理编码
- iOS 2D绘图 (Quartz2D)之路径(stroke,fill,clip,subpath,blend)
- (视频) 开源,免费和跨平台 - MVP ComCamp 2015 KEYNOTE
- Linux中exec()执行文件系列函数的使用说明
- 11g添加asm
- jQuery Questions:Front-end Developer Interview Questions
- WPF:窗体置顶
- Sublime Text2一些快捷键收藏
- CSS skills: 3) show sub-navigate items when mouse hove on nav-item
- 【转】 COCOS2D-X之使用CURL下载图片的一个简单Demo
- HDU 1018 Big Number
- Linux下rsync增加SSH端口号的用法
- java实现冒泡排序,选择排序,插入排序,快速排序(简洁版)及性能测试
- 马丁 福勒 Martin Fowler 关于依赖注入和反转控制的区别
- 内功心法 -- Java中的深拷贝和浅拷贝
- 【Java框架型项目从入门到装逼】第九节 - 数据库建表和CRUD操作
- 嫁给程序员的好处,你get到了吗?
- reload() 函数
- SecureCRT &; SecureFx 绿色破解版
- Json4:使用json-lib解析、生成Json
热门文章
- html5--2.6新的布局元素(5)-nav
- SpringMVC框架<;mvc:default-servlet-handler/>;的作用
- spring属性注入DI
- (转)linux 打开文件数 too many open files 解决方法
- c# json 排版
- leetcode 104 Maximum Depth of Binary Tree(DFS)
- Java多线程加强
- AndroidStudio启动时不自动打开项目
- 条款39:明智而审慎的使用private继承
- 《SpringBoot揭秘 快速构建微服务体系》读后感(二)