Java并发编程(一)JUC同步类

Java77

JUC 是学习 Java 并发编程的小伙伴不可避免的一个 pkg,JUC提供了对并发编程的底层支持,比如我们熟悉的线程池、MQ、线程同步... 都有JUC的影子,下面我们一起来看看JUC下比较重要的几个class。

CountdownLatch

先看一下 latch 是什么意思:
Java并发编程(一)JUC同步类
你们都知道门闩。假设我们现在有一扇铁门。每条线跑到铁门时都会脱下门闩。当所有锁闩都从门上取下时,门打开,线程可以继续执行。

[En]

You all know the latch. Suppose we have an iron door now. Each thread takes off a latch when it runs to the iron door. When all the latches are taken off the door, the door opens and the thread can continue to execute.

```java
public class CountdownLatchTest {
public static void main(String[] args) throws InterruptedException {
// 传入放多少个门闩
CountDownLatch cdl = new CountDownLatch(10);

    // 启 10 个线程
    for (int i = 0; i < 10; i++) {
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " 取下一把门闩");
            // 每个线程运行完就取下一把门闩
            cdl.countDown();
输入验证码查看隐藏内容

扫描二维码关注本站微信公众号 Johngo学长
或者在微信里搜索 Johngo学长
回复 svip 获取验证码
wechat Johngo学长