CountDownLatch 简介和例子

CountDownLatch 简介和例子public class CountDownLat public static int numberOfPeop 10 等车的学生数 public static boolean isGone false 车开的标志 public static int carWaitTime 3

大家好,我是讯享网,很高兴认识大家。 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;
         }

    }

}
小讯
上一篇 2025-02-26 20:30
下一篇 2025-02-05 20:05

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/95617.html