CountDownLatch 简介和例子

public   class  CountDownLatchTest  {
    public static int numberOfPeople = 10;//等车的学生数
    public static boolean isGone = false;//车开的标志
    public static int carWaitTime = 3;//车等的时间
    
    public static void main(String[] args) throws InterruptedException {
        
        CountDownLatch waitStudentsGetOn = new CountDownLatch(numberOfPeople);
        
        new Thread(new GetOn(waitStudentsGetOn)).start();
        
        waitStudentGetOn(waitStudentsGetOn);//等所有的学生上车
        
        driveHome();//开车走
        
    }

    
    private static void waitStudentGetOn(CountDownLatch waitStudentsGetOn) throws InterruptedException {
        System.out.println("赶紧的,抓紧时间上车..");
        waitStudentsGetOn.await(carWaitTime, TimeUnit.SECONDS);//等5秒,还没上车,就开走。。
        
    }


    private static void driveHome() throws InterruptedException {
        System.out.println("开车,鞋儿破 帽儿破 身上的袈裟破 你笑我 他笑我 一把扇儿破");
        isGone = true;
        
    }

    
}

class  GetOn  implements  Runnable {
    
    private CountDownLatch waitStudentsGetOn;
    GetOn(CountDownLatch waitStudentsGetOn){
        this.waitStudentsGetOn = waitStudentsGetOn;
    }

    public void run() {
        for (int i = 0; i < CountDownLatchTest.numberOfPeople; i++) {
            try {
                if(CountDownLatchTest.isGone){
                    System.out.println("妈的,还差:"+waitStudentsGetOn.getCount()+" 个没娃上车呢.怎么车走了");
                    break;
                }

                boolean goonSuccess = new Student(i+1).getOn();//顺序上车
                if(goonSuccess)waitStudentsGetOn.countDown();
            }
 catch (InterruptedException e) {}
                if(waitStudentsGetOn.getCount()!=0l){
                    System.out.println("还差:"+(waitStudentsGetOn.getCount())+" 个没上车");
                }
else{
                    System.out.println("都上车了");
                }

        }

        
        
    }

    class Student{
        private int myNum;//学生编号
         public Student(int num){
                this.myNum = num;
          }

         //上车
         public boolean getOn() throws InterruptedException{
             Thread.currentThread().sleep(new Random().nextInt(2)*1000);//上车使用的时间,随机
             if(CountDownLatchTest.isGone){
                 return false;//不能上了,上车失败
             }

             System.out.print("编号为:"+myNum+"的同学上车了..");
             return true;
         }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值