awaitTermination() shutdown()
来源:互联网 发布:java中线程的生命周期 编辑:程序博客网 时间:2024/05/03 20:51
newFixedThreadPool
创建一个固定大小的线程池。
shutdown():用于关闭启动线程,如果不调用该语句,jvm不会关闭。
awaitTermination():用于等待子线程结束,再继续执行下面的代码。该例中我设置一直等着子线程结束。
- public class Test {
- public static void main(String[] args) throws IOException, InterruptedException {
- ExecutorService service = Executors.newFixedThreadPool(2);
- for (int i = 0; i < 4; i++) {
- Runnable run = new Runnable() {
- @Override
- public void run() {
- System.out.println("thread start");
- }
- };
- service.execute(run);
- }
- service.shutdown();
- service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
- System.out.println("all thread complete");
- }
- }
public class Test {public static void main(String[] args) throws IOException, InterruptedException {ExecutorService service = Executors.newFixedThreadPool(2);for (int i = 0; i < 4; i++) {Runnable run = new Runnable() {@Overridepublic void run() {System.out.println("thread start");}};service.execute(run);}service.shutdown();service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);System.out.println("all thread complete");}}
输出:
thread start
thread start
thread start
thread start
all thread complete
thread start
thread start
thread start
all thread complete
- awaitTermination() shutdown()
- awaitTermination() shutdown()
- 线程池-shutdown和awaitTermination
- 多线程awaitTermination和shutdown的使用问题
- ExecutorService——shutdown方法和awaitTermination方法
- Java线程池关闭2-shutdown和awaitTermination
- threadPoolExecutor 中的 shutdown() 、 shutdownNow() 、 awaitTermination() 的用法和区别
- Android线程池(八)shutdown、awaitTermination、shutdownNow
- shutdown
- Shutdown
- Shutdown
- ShutDown
- shutdown
- shutdown
- shutdown
- shutdown
- shutdown
- shutdown
- 让 IPhone 程序的调试信息只在 debug 模式下输出
- JavaScript写excel
- 自定义消息的步骤
- 枚举的转换怎么做
- 如果依赖的库文件是由低版本gcc编译的怎么办
- awaitTermination() shutdown()
- frame-field 概述
- ios5 自定义导航条问题 UINavigationBar
- Google Chrome下重新打开最近关闭到标签页
- 数据库字符集问题
- Linux 软件包管理
- 10.IDA-基本操作
- MySQL设置唯一键
- DedeEIMS如何实现中英文调用不同排版主页/栏目页面