例子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());    }

最新文章

  1. iOS 原生地图地理编码与反地理编码
  2. iOS 2D绘图 (Quartz2D)之路径(stroke,fill,clip,subpath,blend)
  3. (视频) 开源,免费和跨平台 - MVP ComCamp 2015 KEYNOTE
  4. Linux中exec()执行文件系列函数的使用说明
  5. 11g添加asm
  6. jQuery Questions:Front-end Developer Interview Questions
  7. WPF:窗体置顶
  8. Sublime Text2一些快捷键收藏
  9. CSS skills: 3) show sub-navigate items when mouse hove on nav-item
  10. 【转】 COCOS2D-X之使用CURL下载图片的一个简单Demo
  11. HDU 1018 Big Number
  12. Linux下rsync增加SSH端口号的用法
  13. java实现冒泡排序,选择排序,插入排序,快速排序(简洁版)及性能测试
  14. 马丁 福勒 Martin Fowler 关于依赖注入和反转控制的区别
  15. 内功心法 -- Java中的深拷贝和浅拷贝
  16. 【Java框架型项目从入门到装逼】第九节 - 数据库建表和CRUD操作
  17. 嫁给程序员的好处,你get到了吗?
  18. reload() 函数
  19. SecureCRT &amp; SecureFx 绿色破解版
  20. Json4:使用json-lib解析、生成Json

热门文章

  1. html5--2.6新的布局元素(5)-nav
  2. SpringMVC框架&lt;mvc:default-servlet-handler/&gt;的作用
  3. spring属性注入DI
  4. (转)linux 打开文件数 too many open files 解决方法
  5. c# json 排版
  6. leetcode 104 Maximum Depth of Binary Tree(DFS)
  7. Java多线程加强
  8. AndroidStudio启动时不自动打开项目
  9. 条款39:明智而审慎的使用private继承
  10. 《SpringBoot揭秘 快速构建微服务体系》读后感(二)