Java8-Executors-No.01
2024-08-30 23:08:32
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");
}
}
}
最新文章
- H5全屏滚动专题页最佳实践
- android4.4.2内核移植3.4.1
- [转]控制反转(IOC)和依赖注入(DI)
- Git客户端(Windows系统)的使用
- HDU 1557 权利指数 国家压缩 暴力
- 第四篇:Web框架 - Django
- 宝宝巴士-自动化团队-纵世科技-Wiger-原创分享-QQ:18630195
- ReactiveSwift源码解析(二) Bag容器的代码实现
- linux之egrep命令
- 彻底理解Java中的基本数据类型转换(自动、强制、提升)
- vue.js开发SPA常见问题及解决方法
- LOJ#2427. 「POI2010」珍珠项链 Beads
- LOADRUNNER重装经验
- js文字滚动效果
- Locust性能测试1-环境准备与基本使用
- BZOJ4415: [Shoi2013]发牌 树状数组+二分
- Quartz.NET+Topshelf 创建Windows服务
- ftp命令行工具如何 连接 非标准21端口(其他端口)的ftp服务器
- html实现时间输入框
- PHP项目学习2
热门文章
- stub_status监控Nginx使用情况!
- Mysql: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
- [转帖]年度网络攻击大调查:SSH端口最易受网络攻击,HTTPS其次!
- javascript策略模式的应用!
- Feign【开启GIZP压缩】
- GC(Garbage Collection)
- poj 3069 继续弱鸡的贪心
- 点标记(lambda表达式+linq查询标记符)与linq语句(查询表达式)
- 使用jdk8 stream简化集合操作
- Java Web-Filter and listener