import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; public class Executors1 { public static void main(String[] args) {
test1(3);
// test1(7);
} private static void test1(long seconds) {
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
try {
TimeUnit.SECONDS.sleep(seconds);
String name = Thread.currentThread().getName();
System.out.println("task finished: " + name);
}
catch (InterruptedException e) {
System.err.println("task interrupted");
}
});
stop(executor);
} static void stop(ExecutorService executor) {
try {
System.out.println("attempt to shutdown executor");
executor.shutdown();
executor.awaitTermination(5, TimeUnit.SECONDS);
}
catch (InterruptedException e) {
System.err.println("termination interrupted");
}
finally {
if (!executor.isTerminated()) {
System.err.println("killing non-finished tasks");
}
executor.shutdownNow();
System.out.println("shutdown finished");
}
}
}

最新文章

  1. H5全屏滚动专题页最佳实践
  2. android4.4.2内核移植3.4.1
  3. [转]控制反转(IOC)和依赖注入(DI)
  4. Git客户端(Windows系统)的使用
  5. HDU 1557 权利指数 国家压缩 暴力
  6. 第四篇:Web框架 - Django
  7. 宝宝巴士-自动化团队-纵世科技-Wiger-原创分享-QQ:18630195
  8. ReactiveSwift源码解析(二) Bag容器的代码实现
  9. linux之egrep命令
  10. 彻底理解Java中的基本数据类型转换(自动、强制、提升)
  11. vue.js开发SPA常见问题及解决方法
  12. LOJ#2427. 「POI2010」珍珠项链 Beads
  13. LOADRUNNER重装经验
  14. js文字滚动效果
  15. Locust性能测试1-环境准备与基本使用
  16. BZOJ4415: [Shoi2013]发牌 树状数组+二分
  17. Quartz.NET+Topshelf 创建Windows服务
  18. ftp命令行工具如何 连接 非标准21端口(其他端口)的ftp服务器
  19. html实现时间输入框
  20. PHP项目学习2

热门文章

  1. stub_status监控Nginx使用情况!
  2. Mysql: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
  3. [转帖]年度网络攻击大调查:SSH端口最易受网络攻击,HTTPS其次!
  4. javascript策略模式的应用!
  5. Feign【开启GIZP压缩】
  6. GC(Garbage Collection)
  7. poj 3069 继续弱鸡的贪心
  8. 点标记(lambda表达式+linq查询标记符)与linq语句(查询表达式)
  9. 使用jdk8 stream简化集合操作
  10. Java Web-Filter and listener