Java CountDownLatch
Oracle官方文档
http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CountDownLatch.html
package java.util.concurrent.CountDownLatch;
A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.
A CountDownLatch is initialized with a given count. The await methods block until the current count reaches zero due to invocations of the countDown() method, after which all waiting threads are released and any subsequent invocations of await return immediately. This is a one-shot phenomenon – the count cannot be reset. If you need a version that resets the count, consider using a CyclicBarrier.
A CountDownLatch is a versatile synchronization tool and can be used for a number of purposes. A CountDownLatch initialized with a count of one serves as a simple on/off latch, or gate: all threads invoking await wait at the gate until it is opened by a thread invoking countDown(). A CountDownLatch initialized to N can be used to make one thread wait until N threads have completed some action, or some action has been completed N times.
A useful property of a CountDownLatch is that it doesn’t require that threads calling countDown wait for the count to reach zero before proceeding, it simply prevents any thread from proceeding past an await until all threads could pass.
一个同步的辅助工具类,它允许一个或多个线程等待直到一系列的操作在其它的线程中完成执行。
CountDownLatch使用给定的总数来初始化,await方法会阻塞直到由于调用了countDown方法当前的数量达到零,之后所有的等待线程都会被唤醒并且任何后续await的调用都会立即返回。该类是一次性的 —— 它的数目不能被重置。如果你需要一个可以重置总数的版本,可以考虑使用CyclicBarrier。
CountDownLatch是一个多用途的同步工具,可以被用于实现多种目的。CountDownLatch使用一个服务计数器来初始化,它就像一个简单的开关或者门:所有的线程都调用await方法在门前等待,直到门被某个调用countDown方法的线程打开。CountDownLatch使用N来初始化可以让某个线程等待其它N个线程执行完某动作,或者某个动作被执行完N次。
CountDownLatch有一个有用的属性是它不一定需要其他线程调用countDown,直到数目到达零才能继续执行,它只是简单的防止某些线程提前执行,让它们等待一会直到所有的线程都可以通过。
CountDownLatch 介绍
一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。用给定的计数 初始化 CountDownLatch。由于调用了 countDown() 方法,所以在当前计数到达零之前,await 方法会一直受阻塞。之后,会释放所有等待的线程,await 的所有后续调用都将立即返回。这种现象只出现一次——计数无法被重置。 一个线程(或者多个), 等待另外N个线程完成某个事情之后才能执行。
使用场景
在一些应用场合中,需要等待某个条件达到要求后才能做后面的事情;同时当线程都完成后也会触发事件,以便进行后面的操作。 这个时候就可以使用CountDownLatch。CountDownLatch最重要的方法是countDown()和await(),前者主要是倒数一次,后者是等待倒数到0,如果没有到达0,就只有阻塞等待了。
代码实战
package com.demo.test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CountDownLatchDemo {
// 模拟了100米赛跑,10名选手已经准备就绪,只等裁判一声令下。当所有人都到达终点时,比赛结束
public static void main(String[] args) {
final int COUNT = 10;
final CountDownLatch beginLatch = new CountDownLatch(1);
final CountDownLatch endLatch = new CountDownLatch(COUNT);
ExecutorService executorService = Executors.newFixedThreadPool(COUNT);
for (int i = 0; i < COUNT; i++) {
final int NO = i + 1;
executorService.execute(new Runnable() {
@Override
public void run() {
try {
beginLatch.await();
Thread.sleep((long) (Math.random() * 1000));
System.out.println("No." + NO + " arrived");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
endLatch.countDown();
}
}
});
}
System.out.println("Game Start");
//beginLatch减去1,正好为0,所有await方法返回,即比赛开始
beginLatch.countDown();
//等待endLatch变为0之后返回,即所有选手到达终点
try {
endLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Game Over");
}
}
运行输出
Game Start
No.3 arrived
No.7 arrived
No.4 arrived
No.5 arrived
No.2 arrived
No.10 arrived
No.9 arrived
No.8 arrived
No.6 arrived
No.1 arrived
Game Over
相关文章
- java虚拟机学习-Java常量池理解与总结(13-2)
- Java 并发工具包 java.util.concurrent 用户指南
- Java反射机制详解(3) -java的反射和代理实现IOC模式 模拟spring
- java并发中CountDownLatch的使用
- Word处理控件Aspose.Words功能演示:从 Java 中的 Word DOC 中提取图像
- java基础—计算: 2012-3-17"到"2012-4-6"中间有多少天?
- Java Date Time 教程-java.sql.Date
- Java CopyOnWriteArrayList class
- Java项目(前端vue后台java微服务)在线考试系统(java+vue+springboot+mysql+maven)
- 第二十七节:Java基础面向对象-静态,单例模式,继承详情知识点
- Java Web 开发进阶案例之人事管理系统的完整实现
- JAVA好书之《深入理解Java虚拟机》
- java中“53”个关键字(含2个保留字)
- Java多线程之---用 CountDownLatch 说明 AQS 的实现原理
- JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)
- Java_java动态编译整个项目,解决jar包找不到问题
- Java设计模式之结构型:代理模式
- java基础之装箱和拆箱
- Java学习---JAVA的类设计
- 【JAVA】Exception in thread "main" java.lang.NoClassDefFoundError
- Java泛型的协变
- Java NIO Channel to Channel Transfers
- JAVA并发编程学习笔记之CAS操作
- 2014-5-22 java.lang.OutOfMemoryError: Java heap space的一次诊断
- Java hutool/java 常用方法
- Java SpringBoot 创建第一个应用程序 helloword