zoukankan      html  css  js  c++  java
  • Java实现主线程等待子线程

    本文介绍两种主线程等待子线程的实现方式,以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...

  • 相关阅读:
    一些鲜为人知的编程真相
    一些鲜为人知的编程真相
    Ruby 1.9不会杀死Python
    Boost.Asio和ACE之间关于Socket编程的比较
    Effective C++第17条:要在单独的语句中使用智能指针来存储由new创建的对象
    Ruby 1.9不会杀死Python
    Boost智能指针——weak_ptr
    对象生死劫 - 构造函数和析构函数的异常
    Protocol Buffers:Google 的数据交换格式
    根据出生日期计算年龄
  • 原文地址:https://www.cnblogs.com/toSeeMyDream/p/6959223.html
Copyright © 2011-2022 走看看