In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to achieve Phaser synchronization barrier in Java multithreading. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Preface
Whether you will encounter a situation where you submit multiple tasks to the thread pool and you hope to notify you in reverse when all the tasks are completed.
You may use thread counting until the counter accumulates to the number of threads submitted, and then notify. Emmm, it's not impossible, it's just not elegant. This article provides an elegant implementation, the Phaser synchronization barrier.
Maven dependence
Or you don't have to rely on it. I'm used to simplifying the code and using hutool, so I rely on it.
Cn.hutool hutool-all 5.7.15 Code
Don't talk too much nonsense, put on the code.
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: * Multithreaded Monitoring callback tool @ 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);}} / * submit task and content to be executed after completion * * @ param tasks task * @ param complete completion task * / public static void submit (List tasks, Runnable complete) {Phaser phaser = new CustomPharser (complete) For (Runnable runnable: tasks) {executorService.submit (()-> {phaser.register (); runnable.run (); System.out.println (Thread.currentThread (). GetName () + "get the job done!") ; phaser.arriveAndAwaitAdvance ();});} / * * destroy thread pool * / public static void destroy () {System.out.println (destroy thread pool); executorService.shutdown ();} public static void main (String [] args) {List 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 ("all tasks completed"); ThreadUtil.sleep (20, TimeUnit.SECONDS); destroy ();}}
Code description
1. Whether you submit the task for execution by Runnable, Callable, Consumer, Function, etc., it doesn't affect it. You can watch the adjustment.
2. The completed Runnable is the same as the first point.
Verify it.
OK, there's no problem.
On how to achieve Java multithreading Phaser synchronization barrier to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.