码迷,mamicode.com
首页 > 编程语言 > 详细

Java实现主线程等待子线程

时间:2017-06-07 22:27:40      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:replace   初始化   线程   array   bsp   数值   exception   tac   ring   

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

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

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

join方式代码如下:

 

[java] view plain copy
 
  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] view plain copy
 
  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...

Java实现主线程等待子线程

标签:replace   初始化   线程   array   bsp   数值   exception   tac   ring   

原文地址:http://www.cnblogs.com/toSeeMyDream/p/6959223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!