码迷,mamicode.com
首页 > 其他好文 > 详细

4 CountDownLatch

时间:2016-09-26 21:33:11      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

(1)private static CountDownLatch latch = new CountDownLatch(3);         

(2)latch.countDown();             

(3)latch.await();

 

import java.util.concurrent.CountDownLatch;

public class text{

   /**      * 模拟爸爸去饭店      */     

  public static void fatherToRes() {         

    System.out.println("爸爸步行去饭店需要3小时。");     

  }       

  /**      * 模拟我去饭店      */     

  public static void motherToRes() {         

    System.out.println("妈妈挤公交去饭店需要2小时。");     

  }       

  /**      * 模拟妈妈去饭店      */     

  public static void meToRes() {         

    System.out.println("我乘地铁去饭店需要1小时。");     

  }       

  /**      * 模拟一家人到齐了      */     

  public static void togetherToEat() {         

    System.out.println("一家人到齐了,开始吃饭");        

  }        

  private static CountDownLatch latch = new CountDownLatch(3);         

  public static void main(String[] args) throws InterruptedException {           

    new Thread() {             

      public void run() {                 

        fatherToRes();                 

        latch.countDown();             

      };         

    }.start();         

    new Thread() {             

      public void run() {                 

        motherToRes();                 

        latch.countDown();             

      ;         

    }.start();         

    new Thread() {             

      public void run(){                 

        meToRes();                 

        latch.countDown();             

      };         

    }.start();           

    latch.await();         

    togetherToEat();     

  } 

}

4 CountDownLatch

标签:

原文地址:http://www.cnblogs.com/JaneSJ/p/5910622.html

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