閱讀232 返回首頁    go 小米 go 小米6


關於CountDownLatch類Demo代碼

 

package test;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchDemo {

 /**
  *
  * @author Administrator/2012-3-1/上午09:57:05
  */
 public static void main(String[] args) {

  
  int threadNumber = 10;  
        final CountDownLatch countDownLatch = new CountDownLatch(threadNumber);  
        for (int i = 0; i < threadNumber; i++) {  
            final int threadID = i;   
            new Thread() {  
                public void run() {  
                    try {  
                        Thread.sleep((long) (Math.random() * 10000));  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                    System.out.println(String.format("threadID:[%s] finished!!", threadID));  
                    countDownLatch.countDown();  
                }  
            }.start();  
        }  
          
          
        try {
   countDownLatch.await();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }  
        System.out.println("main thread finished!!");  


 }

}

最後更新:2017-04-02 22:16:29

  上一篇:go java對象拷貝——PropertyUtils.copyProperties()用法和性能
  下一篇:go Android framework/base 下添加新的接口