CountDownLatch

含义:

CountDownLatch可以理解为一个计数器在初始化时设置初始值,当一个线程需要等待某些操作先完成时,需要调用await()方法。这个方法让线程进入休眠状态直到等待的所有线程都执行完成。每调用一次countDown()方法内部计数器减1,直到计数器为0时唤醒。这个可以理解为特殊的CyclicBarrier。线程同步点比较特殊,为内部计数器值为0时开始。

方法:

核心方法两个:countDown()和await()

countDown():使CountDownLatch维护的内部计数器减1,每个被等待的线程完成的时候调用

await():线程在执行到CountDownLatch的时候会将此线程置于休眠

例子

开会的例子:会议室里等与会人员到齐了会议才能开始。

import java.util.concurrent.CountDownLatch;

public class VideoConference implements Runnable {

private final CountDownLatch controller;

public VideoConference(int number) {

controller = new CountDownLatch(number);

}

public void arrive(String name) {

System.out.printf("%s has arrived.\n", name);

controller.countDown();// 调用countDown()方法,使内部计数器减1

System.out.printf("VideoConference: Waiting for %d participants.\n", controller.getCount());

}

@Override

public void run() {

System.out.printf("VideoConference: Initialization: %d participants.\n", controller.getCount());

try {

controller.await();// 等待,直到CoutDownLatch计数器为0

System.out.printf("VideoConference: All the participants have come\n");

System.out.printf("VideoConference: Let's start...\n");

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

参加会议人员类

import java.util.concurrent.Semaphore;

import java.util.concurrent.TimeUnit;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

public class PrintQueue {

//信号量

private Semaphore semaphore;

//是否空闲打印机

private boolean freePrinters[];

private Lock lockPrinters;

public PrintQueue(){

//初始化三个信号

semaphore=new Semaphore(3);

//三台空闲打印机

freePrinters=new boolean[3];

for (int i=0; i<3; i++){

freePrinters[i]=true;

}

lockPrinters=new ReentrantLock();

}

public void printJob (Object document){

try {

//获取信号量

semaphore.acquire();

int assignedPrinter=getPrinter();

Long duration=(long)(Math.random()*10);

System.out.printf("%s: PrintQueue: Printing a Job in Printer %d during %d seconds\n",Thread.currentThread().getName(),assignedPrinter,duration);

TimeUnit.SECONDS.sleep(duration);

freePrinters[assignedPrinter]=true;

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

// Free the semaphore

semaphore.release();

}

}

private int getPrinter() {

int ret=-1;

try {

lockPrinters.lock();

for (int i=0; i<freePrinters.length; i++) {

if (freePrinters[i]){

ret=i;

freePrinters[i]=false;

break;

}

}

} catch (Exception e) {

e.printStackTrace();

} finally {

lockPrinters.unlock();

}

return ret;

}

}

测试类:

public class CountDownLatchMain {

public static void main(String[] args) {

VideoConference conference = new VideoConference(10);

Thread threadConference = new Thread(conference);

threadConference.start();// 开启await()方法,在内部计数器为0之前线程处于等待状态

for (int i = 0; i < 10; i++) {

Participant p = new Participant(conference, "Participant " + i);

Thread t = new Thread(p);

t.start();

}

}

}

海量视频获取 springboot springcloud

最新文章

  1. easyUI在IE浏览器中列表不显示
  2. JavaScript实现TwoQueues缓存模型
  3. 【UE4游戏开发】安装UE4时报SU-PQR1603错误的解决方法
  4. Java8的伪共享和缓存行填充--@Contended注释
  5. 【Selenium】3.介绍Selenium IDE
  6. 【C语言学习】-08 指针
  7. uwsgi安装过程中遇到的问题
  8. SparkGraphXTest.scala
  9. C#删除数组元素代码
  10. SQL2008无法连接到.,及sa登录失败的总结
  11. c语言的typedef
  12. 关于Python的函数(Method)与方法(Function)
  13. 线性一致性与全序广播------《Designing Data-Intensive Applications》读书笔记12
  14. pytorch .detach() .detach_() 和 .data用于切断反向传播
  15. es6 新增数据类型Symbol
  16. 从零开始学 Web 之 HTML5(三)网络监听,全屏,文件读取,地理定位接口,应用程序缓存
  17. 20145101《Java程序设计》第5周学习总结
  18. Redis配置及使用
  19. Javascript:window.close()不起作用?
  20. c++计算器后续(3)

热门文章

  1. PHP定界符&lt;&lt;&lt;的使用方法
  2. AJAX 向后台发送带 List 集合的对象(转)
  3. AMS5601的ardunio和STM32驱动开发
  4. IT书单-持续更新
  5. 【Luogu】【关卡2-6】贪心(2017年10月)
  6. Java优化性能
  7. Windows——关于Word2016/2019提示需要修复问题处理
  8. nginx进行获取阿里云slb真实ip配置操作
  9. 获取微信企业的corpID,sercret,access_token,部门设置列表
  10. python -jieba 安装+分词+定位