主线程统计子线程执行时间---countDownLatch

来源:互联网 发布:打谱软件overture教程 编辑:程序博客网 时间:2024/05/20 19:29
  1. import java.util.*;  
  2. import java.util.concurrent.CountDownLatch;  
  3. public class Test {     
  4.    
  5.     public static void main(String[] args) {  
  6.         long startTime= System.currentTimeMillis();//开始时间  
  7.         int threadNumber = 2;     
  8.         final CountDownLatch countDownLatch = new CountDownLatch(threadNumber);     
  9.   
  10.         new Thread(new Runnable(){  
  11.             public void run(){  
  12.                 for(int i=0;i<1000;i++){  
  13.                     System.out.println("A:"+i);  
  14.                 }  
  15.                 countDownLatch.countDown();  
  16.             }  
  17.         }).start();  
  18.   
  19.         new Thread(new Runnable(){            
  20.             public void run() {  
  21.                 for(int j=0;j<1000;j++){  
  22.                     System.out.println("B:"+j);       
  23.                 }  
  24.                 countDownLatch.countDown();  
  25.             }  
  26.         }).start();     
  27.          try {  
  28.             countDownLatch.await();  
  29.         } catch (InterruptedException e) {  
  30.             // TODO Auto-generated catch block  
  31.             e.printStackTrace();  
  32.         }     
  33.   
  34.         System.out.println("执行总时间:"+(System.currentTimeMillis()-startTime));  
  35.     }  
  36. }  
阅读全文
0 0