当前位置: 首页 > news >正文

常用的辅助类(必会)

1.CountDownLatch

package com.kuang.add;import java.util.concurrent.CountDownLatch;//计数器  减法
public class CountDownLatchDemo {public static void main(String[] args) throws InterruptedException {//总数是6,必须要执行任务的时候,再使用CountDownLatch countDownLatch = new CountDownLatch(6);for (int i = 0; i < 6; i++) {int finalI = i;new Thread(()->{try {System.out.println(Thread.currentThread().getName()+" Go Out");} catch (Exception e) {e.printStackTrace();} finally {countDownLatch.countDown();}},String.valueOf(i)).start();}countDownLatch.await();//等待计数器归零,然后再向下执行System.out.println("close Door");}}

2.CyclicBarrier

package com.kuang.add;import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;public class CyclicBarrierDemo {public static void main(String[] args) {/*** 集齐7颗龙珠 召唤神龙*///召唤龙珠的线程CyclicBarrier cyclicBarrier= new CyclicBarrier(7,()->{System.out.println("召唤神龙成功");});for (int i = 0; i < 6; i++) {int finalI = i;new Thread(()->{System.out.println(Thread.currentThread().getName()+"收集了"+ finalI+"个龙珠");try {cyclicBarrier.await();//等待} catch (InterruptedException e) {e.printStackTrace();} catch (BrokenBarrierException e) {e.printStackTrace();}}).start();}}}

 

3.Semaphore

Semaphore :信号量

package com.kuang.add;import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;public class SemaphoreDemo {public static void main(String[] args) {//线程数量,停车位!Semaphore semaphore = new Semaphore(3);for (int i = 0; i < 6; i++) {new Thread(()->{//acquire()得到许可try {semaphore.acquire();System.out.println(Thread.currentThread().getName()+"抢到车位");TimeUnit.SECONDS.sleep(2);System.out.println(Thread.currentThread().getName()+"离开车位");} catch (InterruptedException e) {e.printStackTrace();}finally {semaphore.release();//release()  释放}},String.valueOf(i)).start();}}
}

原理:

semaphore.acquire(); 获得,假设如果已经满了,等待,等待被释放为止!
semaphore.release(); 释放,会将当前的信号量释放+1,然后唤醒等待线程!

作用:多个共享资源互斥的使用!并发限流。控制最大的线程数!

http://www.lryc.cn/news/168720.html

相关文章:

  • Java常用类之 String、StringBuffer、StringBuilder
  • linux在所有文件中查找某一个字符串
  • WebSocket vs SSE: 实时数据推送到前端的选择与实现(详细)
  • Redis从入门到精通(二:数据类型)
  • 基于SSM的珠宝首饰交易平台
  • 4款视频号数据分析平台!
  • 【系统架构】什么是集群?为什么要使用集群架构?
  • Java手写拓扑排序和拓扑排序应用拓展案例
  • 练习:使用servlet显示试卷页面
  • 视频监控系统/视频云存储EasyCVR接入国标GB28181设备无法播放设备录像,是什么原因?
  • 四叶草clover配置工具:Clover Configurator for Mac
  • 计算机网络第四章——网络层(中)
  • 时序分解 | MATLAB实现基于小波分解信号分解分量可视化
  • VMware虚拟化环境搭建
  • Jenkins :添加node权限获取凭据、执行命令
  • 如何实现不同MongoDB实例间的数据复制?
  • 微服务保护-隔离
  • 报错:appium AttributeError: ‘NoneType‘ object has no attribute ‘to_capabilities‘
  • MFC - 一文带你从小白到项目应用(全套1)
  • (2596. 检查骑士巡视方案leetcode,经典深搜)-------------------Java实现
  • Docker 部署 Bitwarden RS 服务
  • python与mongodb交互-->pymongo
  • 【网络】计算机网络基础
  • (1)输入输出函数:cin和cout(2)数学函数:sqrt、pow、sin、cos、tan等
  • ArmSom-W3开发板之PCIE的开发指南(一)
  • Android 13.0 framework修改AlertDialog对话框的button样式
  • 如何使用ArcGIS Pro提取河网水系
  • python pytesseract 中文文字批量识别
  • Python 之plt.plot()的介绍以及使用
  • 自动化生成代码:MyBatis 的 Generator与MyBatis-Plus 的 AutoGenerator