博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java实现主线程等待子线程
阅读量:4508 次
发布时间:2019-06-08

本文共 2220 字,大约阅读时间需要 7 分钟。

本文介绍两种主线程等待子线程的实现方式,以5个子线程来说明:

1、使用Thread的join()方法,join()方法会阻塞主线程继续向下执行。

2、使用.util.concurrent中的CountDownLatch,是一个倒数计数器。初始化时先设置一个倒数计数初始值,每调用一次countDown()方法,倒数值减一,他的await()方法会阻塞当前进程,直到倒数至0。

join方式代码如下:

 

[java]   
 
  1. package com.test.thread;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. public class MyThread extends Thread  
  7. {  
  8.   
  9.     public MyThread(String name)  
  10.     {  
  11.         this.setName(name);  
  12.     }  
  13.   
  14.     @Override  
  15.     public void run()  
  16.     {  
  17.         System.out.println(this.getName() + " staring...");  
  18.   
  19.         System.out.println(this.getName() + " end...");  
  20.     }  
  21.   
  22.     /** 
  23.      * @param args 
  24.      */  
  25.     public static void main(String[] args)  
  26.     {  
  27.         System.out.println("main thread starting...");  
  28.   
  29.         List<MyThread> list = new ArrayList<MyThread>();  
  30.   
  31.         for (int i = 1; i <= 5; i++)  
  32.         {  
  33.             MyThread my = new MyThread("Thrad " + i);  
  34.             my.start();  
  35.             list.add(my);  
  36.         }  
  37.   
  38.         try  
  39.         {  
  40.             for (MyThread my : list)  
  41.             {  
  42.                 my.join();  
  43.             }  
  44.         }  
  45.         catch (InterruptedException e)  
  46.         {  
  47.             e.printStackTrace();  
  48.         }  
  49.   
  50.         System.out.println("main thread end...");  
  51.   
  52.     }  
  53.   
  54. }  

运行结果如下:

 

main thread starting...

Thrad 2 staring...
Thrad 2 end...
Thrad 4 staring...
Thrad 4 end...
Thrad 1 staring...
Thrad 1 end...
Thrad 3 staring...
Thrad 3 end...
Thrad 5 staring...
Thrad 5 end...
main thread end...
CountDownLatch方式代码如下:

 

[java]   
 
  1. package com.test.thread;  
  2.   
  3. import java.util.concurrent.CountDownLatch;  
  4.   
  5. public class MyThread2 extends Thread  
  6. {  
  7.   
  8.     private CountDownLatch count;  
  9.   
  10.     public MyThread2(CountDownLatch count, String name)  
  11.     {  
  12.         this.count = count;  
  13.         this.setName(name);  
  14.     }  
  15.   
  16.     @Override  
  17.     public void run()  
  18.     {  
  19.         System.out.println(this.getName() + " staring...");  
  20.   
  21.         System.out.println(this.getName() + " end...");  
  22.         this.count.countDown();  
  23.     }  
  24.   
  25.     /** 
  26.      * @param args 
  27.      */  
  28.     public static void main(String[] args)  
  29.     {  
  30.         System.out.println("main thread starting...");  
  31.   
  32.         CountDownLatch count = new CountDownLatch(5);  
  33.   
  34.         for (int i = 1; i <= 5; i++)  
  35.         {  
  36.             MyThread2 my = new MyThread2(count, "Thread " + i);  
  37.             my.start();  
  38.         }  
  39.   
  40.         try  
  41.         {  
  42.             count.await();  
  43.         }  
  44.         catch (InterruptedException e)  
  45.         {  
  46.             e.printStackTrace();  
  47.         }  
  48.   
  49.         System.out.println("main thread end...");  
  50.   
  51.     }  
  52.   
  53. }  

运行结果如下:

 

main thread starting...

Thread 2 staring...
Thread 2 end...
Thread 4 staring...
Thread 4 end...
Thread 1 staring...
Thread 1 end...
Thread 3 staring...
Thread 3 end...
Thread 5 staring...
Thread 5 end...
main thread end...

转载于:https://www.cnblogs.com/toSeeMyDream/p/6959223.html

你可能感兴趣的文章
Mysql创建自动生成时间列方法
查看>>
12171131第一次作业
查看>>
argparse
查看>>
Pandas使用to_csv保存中文数据用Excel打开是乱码
查看>>
超大整数计算模拟算法
查看>>
<USACO07JAN>解决问题Problem Solvingの思路
查看>>
docker学习
查看>>
C#3.0入门系列(六)-之OrderBy操作
查看>>
10.25 AITalkUat部署
查看>>
枚举学习
查看>>
阅读《构建之法》第8,9,10章
查看>>
用户体验的重要性
查看>>
第八十一节,CSS3变形效果
查看>>
WordCount 分析
查看>>
项目需求分析答辩总结
查看>>
PostgreSQL 利用pg_upgrade升级版本
查看>>
Mybatis分页插件PageHelper简单使用
查看>>
同步互斥——理发师睡觉问题
查看>>
Python编写的ARP扫描工具
查看>>
github之克隆
查看>>