CyclicBarrier就是要实现有福同享有难同当的原理,吃饭的时候,要等室友都到了才会一起去吃食堂,吃饭食堂一起去教室
每一阶段完成后,才会开始下一阶段
代码部分
package com.java.test.cyclicbarrier; import lombok.extern.slf4j.Slf4j; import java.util.Random; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; /** * @Description: * @Author: Yourheart * @Create: 2023/1/3 17:57 */ @Slf4j public class MyThread extends Thread { private final CyclicBarrier barrier; private final Random random = new Random(); public MyThread(String name, CyclicBarrier barrier) { super(name); this.barrier = barrier; } @Override public void run() { try { log.info(Thread.currentThread().getName() + " - 起床"); Thread.sleep(random.nextInt(6000)); log.info(Thread.currentThread().getName() + " - 洗漱完毕"); //等待其他的室友 barrier.await(); log.info(Thread.currentThread().getName() + " - 食堂打完菜开始吃饭"); Thread.sleep(random.nextInt(6000)); log.info(Thread.currentThread().getName() + " - 吃饭结束"); //等待其他还是吃饭的室友 barrier.await(); log.info(Thread.currentThread().getName() + " - 准备出发去教室"); Thread.sleep(random.nextInt(6000)); log.info(Thread.currentThread().getName() + " - 全部到达教室"); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } } }
package com.java.test.cyclicbarrier; import java.util.concurrent.CyclicBarrier; /** * @Description: * @Author: Yourheart * @Create: 2023/1/3 17:56 */ public class MainTest { public static void main(String[] args) { CyclicBarrier cyclicBarrier=new CyclicBarrier(5, new Runnable() { @Override public void run() { System.out.println("该阶段结束..."); } }); for (int i=0;i<5;i++){ new MyThread("同学-" + (i + 1), cyclicBarrier).start(); } } }
pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.java</groupId> <artifactId>test-study</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> </parent> <dependencies> <!--tomcat容器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--lombok依赖--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> </dependency> <!--引入junit单元测试依赖--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--判断空的用法 --> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8 --> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <version>12.2.0.1</version> </dependency> <!--springboot整合mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <!--添加fastjson依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.70</version> </dependency> <!-- 热部署模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- 这个需要为 true 热部署才有效 --> </dependency> <!--ThreadFactoryBuilder的依赖包,多线程使用--> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>30.1-jre</version> </dependency> <!--Lists.partition要用的依赖--> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>21.0</version> </dependency> <!--ListUtils.partition使用的依赖--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.4</version> </dependency> <!--操作redis的依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <finalName>study</finalName> </build> </project>
相对于CountDownLatch,CyclicBarrier可以重复使用。
标签:Thread,spring,简单,boot,commons,使用,org,CyclicBarrier From: https://www.cnblogs.com/q202105271618/p/16999325.html