Java 多线程等待优雅的实现方式之Phaser同步屏障
作者:剑客阿良_ALiang 时间:2023-11-29 09:47:43
前言
是否会遇到这样的场景,你向线程池提交了多个任务,你希望这批任务全部完成后能够反向通知你。
你可能会使用线程计数的方式,等到计数器累加到提交的线程数量,然后通知。emmm,不是不可以,只是不够优雅。本文提供优雅的实现方式,Phaser同步屏障。
Maven依赖
也可以不依赖,本人习惯把代码简单化,使用了hutool,所以依赖只有这个。
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.15</version>
</dependency>
代码
废话不多说,上代码。
package com.huyi.csdn.tools;
import cn.hutool.core.thread.ThreadUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;
/**
* @Program: csdn @ClassName: PhaserUtil @Author: huyi @Date: 2021-11-06 21:03 @Description:
* 多线程监控回调工具 @Version: V1.0
*/
public class PhaserUtil {
public static final ExecutorService executorService = Executors.newFixedThreadPool(50);
public static class CustomPharser extends Phaser {
private final Runnable runnable;
public CustomPharser(Runnable runnable) {
this.runnable = runnable;
}
@Override
protected boolean onAdvance(int phase, int registeredParties) {
this.runnable.run();
return super.onAdvance(phase, registeredParties);
}
}
/**
* 提交任务以及完成后需要执行的内容
*
* @param tasks 任务
* @param complete 完成任务
*/
public static void submit(List<Runnable> tasks, Runnable complete) {
Phaser phaser = new CustomPharser(complete);
for (Runnable runnable : tasks) {
executorService.submit(
() -> {
phaser.register();
runnable.run();
System.out.println(Thread.currentThread().getName() + "完成任务!");
phaser.arriveAndAwaitAdvance();
});
}
}
/** 摧毁线程池 */
public static void destroy() {
System.out.println("摧毁线程池");
executorService.shutdown();
}
public static void main(String[] args) {
List<Runnable> tasks = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < 10; i++) {
tasks.add(
() -> {
ThreadUtil.sleep(random.nextInt(10), TimeUnit.SECONDS);
});
}
submit(tasks, () -> System.out.println("所有任务已完成"));
ThreadUtil.sleep(20, TimeUnit.SECONDS);
destroy();
}
}
代码说明
1、提交任务执行的方式是Runnable也好,Callable也好,或者Consumer、Function等等,不影响,你可以看着调整。
2、完成后的Runnable也和第一点同理。
验证一下
OK,没什么问题。
来源:https://huyi-aliang.blog.csdn.net/article/details/121185988
标签:Java,多线程等待,Phaser,同步屏障
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#实现在Form里面内嵌dos窗体的方法
2022-04-27 07:13:07
Java装饰器设计模式初探
2021-08-17 17:23:46
![](https://img.aspxhome.com/file/2023/8/84168_0s.jpg)
C#实现餐厅管理系统
2023-11-27 16:05:05
Android通过startService实现文件批量下载
2022-11-02 05:29:52
![](https://img.aspxhome.com/file/2023/3/138113_0s.jpg)
Java中BigDecimal的加减乘除、比较大小与使用注意事项
2022-02-17 21:24:09
![](https://img.aspxhome.com/file/2023/1/66251_0s.jpg)
linux(center OS7)安装JDK、tomcat、mysql 搭建java web项目运行环境
2022-07-04 07:02:37
MyBatis插入Insert、InsertSelective的区别及使用心得
2023-08-25 04:34:28
Android使用PhotoView实现图片双击放大单击退出效果
2022-10-10 04:52:11
java 中http请求为了防止乱码解决方案
2023-08-09 07:59:33
深入了解c# 信号量和互斥体
2022-09-19 11:07:47
![](https://img.aspxhome.com/file/2023/4/113864_0s.png)
Java如何解析html中的内容并存到数据库详解
2023-11-29 05:08:32
Java中IO流解析及代码实例详解
2022-03-08 22:32:16
C#多线程之任务的用法详解
2023-08-27 10:51:18
![](https://img.aspxhome.com/file/2023/9/69209_0s.png)
docker网络配置过程详解介绍
2023-02-07 23:04:15
![](https://img.aspxhome.com/file/2023/4/108274_0s.jpg)
一款域名监控小工具 Domain(IP)Watcher 实现代码
2023-09-15 11:55:44
![](https://img.aspxhome.com/file/2023/6/98256_0s.jpg)
Java基础之反射详解
2022-06-16 12:25:11
![](https://img.aspxhome.com/file/2023/5/61955_0s.png)
Android控件CardView实现卡片布局
2022-12-05 02:10:32
![](https://img.aspxhome.com/file/2023/7/96027_0s.jpg)
SpringBoot可视化监控的具体应用
2023-07-28 20:32:02
![](https://img.aspxhome.com/file/2023/7/94357_0s.png)
SpringBoot2.x过后static下的静态资源无法访问的问题
2023-07-07 00:21:09
![](https://img.aspxhome.com/file/2023/3/67623_0s.jpg)
Springboot如何设置过滤器及重复读取request里的body
2022-05-26 04:59:20
![](https://img.aspxhome.com/file/2023/6/74996_0s.jpg)