JUC-CountDownLatch

2022-3-13 diaba 多线程

package com.jiucaiyuan.net.juc.lock;

import java.util.concurrent.CountDownLatch;

/**
 * 演示CountDownLatch
 * 6个同学陆续离开教室之后,班长才能锁门
 *
 * @Author jiucaiyuan  2022/3/13 22:40
 * @mail services@jiucaiyuan.net
 */

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

        CountDownLatch countDownLatch = new CountDownLatch(6);

        for (int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "号同学离开了教室。");
                countDownLatch.countDown();
            },String.valueOf(i)).start();
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() + "班长锁门离开了教室。");
    }
}

发表评论:

Powered by emlog 京ICP备15045175号-1 Copyright © 2022