1. 程式人生 > >Java實現主執行緒等待子執行緒join,CountDownLatch

Java實現主執行緒等待子執行緒join,CountDownLatch

本文介紹兩種主執行緒等待子執行緒的實現方式,以5個子執行緒來說明:

1、使用Thread的join()方法,join()方法會阻塞主執行緒繼續向下執行。

2、使用Java.util.concurrent中的CountDownLatch,是一個倒數計數器。初始化時先設定一個倒數計數初始值,每呼叫一次countDown()方法,倒數值減一,他的await()方法會阻塞當前程序,直到倒數至0。

join方式程式碼如下:

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

執行結果如下:

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方式程式碼如下:

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

執行結果如下:

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...