JUC辅助类CountDownLatch确保线程有序安全

CountDown计数器减一方法,await方法线程等待阻塞,变为零继续执行

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class Asd {

    public static void main(String []args) throws InterruptedException {
        CountDownLatch cut=new CountDownLatch(6);//juc辅助类
       for(int i=1;i<=6;i++) {
           new Thread(()->{System.out.println(Thread.currentThread().getName()+"号同学离开了教室");
               cut.countDown();},String.valueOf(i)).start();

       }
       cut.await();
       System.out.println(Thread.currentThread().getName()+"班长锁门走人了");
    }
}

上一篇:1891. Cutting Ribbons


下一篇:Linux基础学习(三)