feat: CountDownLatch使用

This commit is contained in:
Bunny 2025-01-20 20:35:27 +08:00
parent 39d2b8b43f
commit d5f197ce34
1 changed files with 20 additions and 0 deletions

View File

@ -1,4 +1,24 @@
package cn.bunny.atomic;
import java.util.concurrent.CountDownLatch;
public class AtomicExample04 {
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(2);
// 此时为1
latch.countDown();
System.out.println(latch.getCount());
// 此时为0
latch.countDown();
System.out.println(latch.getCount());
// 还是为0
latch.countDown();
System.out.println(latch.getCount());
latch.await();
System.out.println(latch.getCount());
}
}