In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Java loop barrier CyclicBarrier how to achieve multi-threaded segments waiting for the completion of execution, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
Preface
Whether there is such a scenario in the work, multiple thread tasks, if all threads complete to a certain stage, you want to know that all threads have completed that stage. Of course you can do it with thread counting, but it's not elegant enough.
So I am: Java multithreading waiting for the Phaser synchronization barrier of elegant implementation
Then provide a loop barrier, CyclicBarrier, a more elegant implementation tool.
Maven dependence
You can rely on it or not, but with a little more code, it's best to add it.
Org.projectlombok lombok true cn.hutool hutool-all 5.7.15 Code
No nonsense, go to the code.
Package com.huyi.csdn.tools; import cn.hutool.core.thread.ThreadUtil;import lombok.Getter;import lombok.Setter; import java.text.MessageFormat;import java.util.ArrayList;import java.util.List;import java.util.Random;import java.util.concurrent.* / * @ Program: csdn @ ClassName: CyclicBarrierUtil @ Author: huyi @ Date: 2021-11-07 17:09 @ Description: * cycle Barrier tool @ Version: V1.0 * / public class CyclicBarrierUtil {public static final ExecutorService executorService = Executors.newFixedThreadPool (50); @ Getter @ Setter public static class SegmentedTask {private Runnable start; private Runnable middle; private Runnable end; public SegmentedTask (Runnable start, Runnable middle, Runnable end) {this.start = start This.middle = middle; this.end = end;}} / * * submit task * * @ param tasks parameter * / public static void submit (List tasks) {CyclicBarrier cyclicBarrier = new CyclicBarrier (tasks.size () + 1) Try {tasks.forEach (x-> {executorService.submit ()-> {try {x.getStart () .run (); cyclicBarrier.await (); x.getMiddle () .run (); cyclicBarrier.await () X.getEnd () .run (); cyclicBarrier.await ();} catch (InterruptedException | BrokenBarrierException e) {e.printStackTrace ();}});}); cyclicBarrier.await (); System.out.println ("initial tasks are all completed") CyclicBarrier.await (); System.out.println ("Intermediate tasks are all completed"); cyclicBarrier.await (); System.out.println ("final tasks are all completed");} catch (InterruptedException | BrokenBarrierException e) {e.printStackTrace ();}} public static void destroy () {System.out.println ("destroy thread pool"); executorService.shutdown ();}}
Code description
1. Construct a segmented task entity class with 3 segments of Runnable.
2. After the completion of different segments of Runnable, they will wait for other threads to complete the task.
Verification code
Public static void main (String [] args) {Random random = new Random (); List tasks = new ArrayList (); for (int I = 1; I
< 11; i++) { int finalI = i; tasks.add( new SegmentedTask( () ->{ThreadUtil.sleep (random.nextInt (10), TimeUnit.SECONDS); System.out.println (Thread.currentThread () + ":" + MessageFormat.format ("the {0} Legion has begun to advance!" , finalI);}, ()-> {ThreadUtil.sleep (random.nextInt (10), TimeUnit.SECONDS); System.out.println (Thread.currentThread () + ":" + MessageFormat.format ("the {0} Legion is already attacking!" , finalI);}, ()-> {ThreadUtil.sleep (random.nextInt (10), TimeUnit.SECONDS); System.out.println (Thread.currentThread () + ":" + MessageFormat.format ("the {0} Legion has captured the position!" , finalI);});} CyclicBarrierUtil.submit (tasks); ThreadUtil.sleep (35, TimeUnit.SECONDS); CyclicBarrierUtil.destroy ();}
Verification result
E:\ Java\ jdk1.8.0_40\ bin\ java.exe "- javaagent:E:\ Program Files\ JetBrains\ IntelliJ IDEA 2020.1.1\ lib\ idea_rt.jar=5831:E:\ Program Files\ JetBrains\ IntelliJ IDEA 2020.1.1\ bin"-Dfile.encoding=UTF-8-classpath E:\ Java\ jdk1.8.0_40\ jre\ lib\ charsets.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ deploy.jar E:\ Java\ jdk1.8.0_40\ jre\ lib\ ext\ access-bridge-64.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ ext\ cldrdata.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ ext\ dnsns.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ ext\ jaccess.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ ext\ jfxrt.jar E:\ Java\ jdk1.8.0_40\ jre\ lib\ ext\ localedata.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ ext\ nashorn.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ ext\ sunec.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ ext\ sunjce_provider.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ ext\ sunmscapi.jar E:\ Java\ jdk1.8.0_40\ jre\ lib\ ext\ sunpkcs11.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ ext\ zipfs.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ javaws.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ jce.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ jfr.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ jfxswt.jar E:\ Java\ jdk1.8.0_40\ jre\ lib\ jsse.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ management-agent.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ plugin.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ resources.jar;E:\ Java\ jdk1.8.0_40\ jre\ lib\ rt.jar;C:\ Users\ yi\ IdeaProjects\ csdn\ target\ classes C:\ Users\ yi\ .m2\ repository\ org\ springframework\ boot\ spring-boot-starter-web\ 2.5.6\ spring-boot-starter-web-2.5.6.jar;C:\ Users\ yi\ .m2\ repository\ org\ springframework\ boot\ spring-boot-starter\ 2.5.6\ spring-boot-starter-2.5.6.jar C:\ Users\ yi\ .m2\ repository\ org\ boot\ spring-boot-starter-logging\ 2.5.6\ spring-boot-starter-logging-2.5.6.jar;C:\ Users\ yi\ .m2\ repository\ ch\ qos\ logback\ logback-classic\ 1.2.6\ logback-classic-1.2.6.jar;C:\ Users\ yi\ .m2\ repository\ ch\ qos\ logback\ logback-core\ 1.2.6\ logback-core-1.2.6.jar C:\ Users\ yi\ .m2\ repository\ org\ apache\ logging\ log4j\ log4j-to-slf4j\ 2.14.1\ log4j-to-slf4j-2.14.1.jar;C:\ Users\ yi\ .m2\ repository\ apache\ logging\ log4j\ log4j-api\ 2.14.1\ log4j-api-2.14.1.jar;C:\ Users\ yi\ .m2\ repository\ org\ slf4j\ jul-to-slf4j\ 1.7.32\ jul-to-slf4j-1.7.32.jar C:\ Users\ yi\ .m2\ repository\ jakarta\ annotation\ jakarta.annotation-api\ 1.3.5\ jakarta.annotation-api-1.3.5.jar;C:\ Users\ yi\ .m2\ repository\ org\ yaml\ snakeyaml\ 1.28\ snakeyaml-1.28.jar;C:\ Users\ yi\ .m2\ repository\ org\ springframework\ boot\ spring-boot-starter-json\ 2.5.6\ spring-boot-starter-json-2.5.6.jar C:\ Users\ yi\ .m2\ repository\ com\ jackson\ core\ jackson-databind\ 2.12.5\ jackson-databind-2.12.5.jar;C:\ Users\ yi\ .m2\ repository\ com\ jackson\ core\ jackson-annotations\ 2.12.5\ jackson-annotations-2.12.5.jar;C:\ Users\ yi\ .m2\ repository\ com\ fasterxml\ jackson\ core\ 2.12.5\ jackson-core-2.12.5.jar C:\ Users\ yi\ .m2\ repository\ com\ jackson\ datatype\ jackson-datatype-jdk8\ 2.12.5\ jackson-datatype-jdk8-2.12.5.jar position C:\ Users\ yi\ .m2\ repository\ com\ fasterxml\ jackson\ datatype\ jackson-datatype-jsr310\ 2.12.5\ jackson-datatype-jsr310-2.12.5.jar C:\ Users\ yi\ .m2\ repository\ com\ fasterxml\ jackson\ module\ jackson-module-parameter-names\ 2.12.5\ jackson-module-parameter-names-2.12.5.jar;C:\ Users\ yi\ .m2\ repository\ org\ springframework\ boot\ spring-boot-starter-tomcat\ 2.5.6\ spring-boot-starter-tomcat-2.5.6.jar C:\ Users\ yi\ .m2\ repository\ org\ tomcat\ embed\ tomcat-embed-core\ 9.0.54\ tomcat-embed-core-9.0.54.jar;C:\ Users\ yi\ .m2\ repository\ org\ apache\ tomcat\ embed\ tomcat-embed-el\ 9.0.54\ tomcat-embed-el-9.0.54.jar C:\ Users\ yi\ .m2\ repository\ org\ tomcat\ embed\ tomcat-embed-websocket\ 9.0.54\ tomcat-embed-websocket-9.0.54.jar;C:\ Users\ yi\ .m2\ repository\ springframework\ spring-web\ 5.3.12\ spring-web-5.3.12.jar;C:\ Users\ yi\ .m2\ repository\ org\ springframework\ spring-beans\ 5.3.12\ spring-beans-5.3.12.jar C:\ Users\ yi\ .m2\ repository\ org\ springframework\ spring-webmvc\ 5.3.12\ spring-webmvc-5.3.12.jar;C:\ Users\ yi\ .m2\ repository\ org\ springframework\ spring-aop\ 5.3.12\ spring-aop-5.3.12.jar;C:\ Users\ yi\ .m2\ org\ springframework\ spring-context\ 5.3.12\ spring-context-5.3.12.jar C:\ Users\ yi\ .m2\ repository\ org\ springframework\ spring-expression\ 5.3.12\ spring-expression-5.3.12.jar;C:\ Users\ yi\ .m2\ repository\ org\ springframework\ boot\ spring-boot-devtools\ 2.5.6\ spring-boot-devtools-2.5.6.jar;C:\ Users\ yi\ repository\ org\ springframework\ boot\ spring-boot\ 2.5.6\ spring-boot-2.5.6.jar C:\ Users\ yi\ .m2\ repository\ org\ boot\ spring-boot-autoconfigure\ 2.5.6\ spring-boot-autoconfigure-2.5.6.jar;C:\ Users\ yi\ .m2\ repository\ org\ springframework\ boot\ 2.5.6\ spring-boot-configuration-processor-2.5.6.jar;C:\ Users\ yi\ .m2\ repository\ org\ projectlombok\ lombok\ 1.18.22\ lombok-1.18.22.jar C:\ Users\ yi\ .m2\ repository\ org\ slf4j\ slf4j-api\ 1.7.32\ slf4j-api-1.7.32.jar;C:\ Users\ yi\ .m2\ repository\ org\ springframework\ spring-core\ 5.3.12\ spring-core-5.3.12.jar;C:\ Users\ yi\ .m2\ org\ springframework\ spring-jcl\ 5.3.12\ spring-jcl-5.3.12.jar C:\ Users\ yi\ .m2\ repository\ cn\ hutool\ hutool-all\ 5.7.15\ hutool-all-5.7.15.jar;C:\ Users\ yi\ .m2\ repository\ com\ google\ guava\ guava\ 31.0.1-jre\ guava-31.0.1-jre.jar;C:\ Users\ yi\ .m2\ repository\ com\ google\ guava\ failureaccess\ 1.0.1\ failureaccess-1.0.1.jar C:\ Users\ yi\ .m2\ repository\ com\ google\ guava\ listenablefuture\ 9999.0-empty-to-avoid-conflict-with-guava\ listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar;C:\ Users\ yi\ .m2\ repository\ com\ google\ code\ findbugs\ jsr305\ 3.0.2\ jsr305-3.0.2.jartz C:\ Users\ yi\ .m2\ repository\ org\ checkerframework\ checker-qual\ 3.12.0\ checker-qual-3.12.0.jar C:\ Users\ yi\ .m2\ repository\ com\ google\ errorprone\ error_prone_annotations\ 2.7.1\ error_prone_annotations-2.7.1.jar;C:\ Users\ yi\ .m2\ repository\ com\ google\ j2objc\ j2objc-annotations\ 1.3\ j2objc-annotations-1.3.jar com.huyi.csdn.tools.CyclicBarrierUtil
Threadpool-1 color threadperceiving 9 minutes 5 main]: the 9th Legion has begun to advance!
Threadpool-1 color threadperceiving 10 minutes 5 main]: the 10th Legion has begun to advance!
Threadpool-1 color threadperceiving 8pi 5 main]: the 8th Legion has begun to advance!
Thread [pool-1 color threadcolor]: the 3rd Legion has begun to advance!
Threadpool-1 color threadperceiving 7 minutes 5 main]: the 7th Legion has begun to advance!
Threadpool-1 color threadperceiving 5pencil main]: the 5th Legion has begun to advance!
Threadpool-1 color threadperceiving 6 minutes 5 main]: the 6th Legion has begun to advance!
Threadpool-1 color threadperceiving 4jin5 main]: the 4th Legion has begun to advance!
Thread [pool-1 color thread color]: the 1st Legion has begun to move forward!
Threadpool-1 color threadmere 2jin5 main]: the 2nd Legion has begun to advance!
The initial task has been completed.
Threadpool-1 color threadhouse 4jin5 main]: the 4th Legion is already attacking!
Threadpool-1 color threadperceiving 6 minutes 5 main]: the 6th Legion is already attacking!
Threadpool-1 color threadperceiving 5pencil main]: the 5th Legion is already attacking!
Threadpool-1 Threadpool-1 color threadperceiving 8 minutes 5 main]: the 8th Legion is already attacking!
Threadpool-1 color threadperceiving 7 minutes 5 main]: the 7th Legion is already attacking!
Threadpool-1 Threadpool-1 color threadperceiving 9 minutes main]: the 9th Legion is already attacking!
Threadpool-1 color threadperceive5 main]: the 1st Legion is already attacking!
Threadpool-1 color threadhouse 2jin5 main]: the 2nd Legion is already attacking!
Threadpool-1 color threadperceiving 3pd5 main]: the 3rd Legion is already attacking!
Threadpool-1 color threadperceiving 10 minutes 5 main]: the 10th Legion is already attacking!
The intermediate tasks have all been completed.
Threadpool-1 color threadhouse 7 minutes 5 main]: the 7th Legion has captured the position!
Threadpool-1 color threadlet 5pi 5je main]: the 5th Legion has captured the position!
Threadpool-1 color threadlet 2jin5 main]: the 2nd Legion has captured the position!
Threadpool-1 Threadpool-1 threadcolor 8pi 5 main]: the 8th Legion has captured the position!
Threadpool-1 color threadperceive 3pd5 main]: the 3rd Legion has captured the position!
Threadpool-1 Threadpool-1 threadperceiving 9 minutes 5 main]: the 9th Legion has captured the position!
Threadpool-1 Threadpool-1 color threadperceiving 10 minutes 5 main]: the 10th Legion has captured the position!
Threadpool-1 color threadlet 4jin5 main]: the 4th Legion has captured the position!
Thread [pool-1 color threadhouse]: the 1st Legion has captured the position!
Threadpool-1 color threadperceiving 6 minutes 5 main]: the 6th Legion has captured the position!
The final task has been completed.
Destroy thread pool
Process finished with exit code 0
OK, perfect.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.