当前位置:网站首页>面试官:多个线程执行完毕后,才执行另一个线程,该怎么做?
面试官:多个线程执行完毕后,才执行另一个线程,该怎么做?
2022-08-04 15:59:00 【51CTO】

CountDownLatch
去掉try catch版本
先来演示一下用法,可以看到所有子线程都执行完毕才会执行主线程。实现这个功能主要靠的是CountDownLatch的2个方法await()和countDown()。
new一个CountDownLatch时会传一个计数器的值,上面的例子为3。调用await()方法时判断计数是否为0,如果不为0则呈等待状态。其他线程可以调用countDown()方法将计数减1,当计数减到位0时,则呈等待的线程继续执行。
CyclicBarrier
去掉try catch版本
CountDownLatch的计数器只能使用一次,而CyclicBarrier的计数器可以使用reset()方法重置。挺简单的就不再演示。因为这2个工具类都用到了AQS,而AQS的原理很长,因此在本文就不介绍AQS的实现了
CompletableFuture
去掉try catch版本
参考博客
https://www.jianshu.com/p/2808c93fef12
边栏推荐
- Li Mu's deep learning notes are here!
- 西安纵横资讯×JNPF:适配中国企业特色,全面集成费用管控体系
- SAP HANA Schemas 和 HDI Containers
- Pulsar消费者处理不当导致的消息积压问题
- 不需要服务器,教你仅用30行代码搞定实时健康码识别
- JVM调优-GC基本原理和调优关键分析
- 重构指标之如何监控代码圈复杂度
- 【Pick-in】Advertising-information flow cross-domain CTR estimation (to be updated)
- Many merchants mall system function and dismantling 24 - ping the strength distribution of members
- C# 写系统日志
猜你喜欢

In action: 10 ways to implement delayed tasks, with code!

Many merchants mall system function and dismantling 24 - ping the strength distribution of members

Typora收费?搭建VS Code MarkDown写作环境

JVM调优-GC基本原理和调优关键分析

西安纵横资讯×JNPF:适配中国企业特色,全面集成费用管控体系

AAAI‘22 推荐系统论文梳理

我说MySQL联合索引遵循最左前缀匹配原则,面试官让我回去等通知

An article to answer what is the product library of the DevOps platform

ICDE‘22推荐系统论文之Research篇

张乐:研发效能的黄金三角及需求与敏捷协作领域的实践|直播回顾
随机推荐
NFT盲盒挖矿系统dapp开发NFT链游搭建
DocuWare Platform - Content Services and Workflow Automation Platform for Document Management (Part 1)
弄懂#if #ifdef #if defined
云存储硬核技术内幕——(10)
Difference between GET and POST requests
荐书 | 《大脑的奥秘:人人要懂的脑科学》:大脑里面有什么
第三章 Scala运算符
74行代码实现浪漫的红心下落的动画效果
云存储硬核技术内幕——(12) 皮洛士惨胜罗马军团
成功 解决 @keyup.enter=“search()“ 在el-input 组件中不生效的问题
Does DMS have an interface to get the list of databases under each instance?
Redis的主从复制和集群
Xi'an Zongheng Information × JNPF: Adapt to the characteristics of Chinese enterprises, fully integrate the cost management and control system
UWP 转换 IBuffer 和其他类型
GET 和 POST 请求的区别
JVM Tuning-GC Fundamentals and Tuning Key Analysis
如何防止重复下单?
【Gopher 学个函数】边学边练,简单为 Go 上个分
分支控制if-else
什么是APS?APS+MES如何解决生产难题?