package com.itjava.interview;
import java.util.concurrent.CountDownLatch;
public class CountDownLatchDemo {
public static void main(String[] args) throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(6); //计数的上锁 做减法
for (int i = 0; i <= 6; i++) {
new Thread(() -> {
System.out.println(Thread.currentThread().getName() + "\t国,被灭");
countDownLatch.countDown(); //上CountDownLatch锁
}, ContryEnum.forEach_ContryEnum(i).getRetMessage()).start(); //使用枚举
}
countDownLatch.await(); //当countDownLatch减为0才可以
//设计主线程最后走
System.out.println(Thread.currentThread().getName() + "\t**********秦帝国,一统华夏!");
System.out.println();
System.out.println(ContryEnum.ONE);
System.out.println(ContryEnum.ONE.getRetCode());
System.out.println(ContryEnum.ONE.getRetMessage());
}
public static void closeDoor() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(6); //计数的上锁 做减法
for (int i = 0; i <= 6; i++) {
new Thread(() -> {
System.out.println(Thread.currentThread().getName() + "\t上完自习,离开教室");
countDownLatch.countDown(); //上CountDownLatch锁
}, String.valueOf(i)).start();
}
countDownLatch.await(); //当countDownLatch减为0才可以
//设计主线程最后走
System.out.println(Thread.currentThread().getName() + "\t**********班长最后关门走人");
}
}