Phaser的简单使用
Phaser属于jdk1.7新增的同步工具类
使用CountDownLatch,主线程执行的任务要等副线程执行完毕才可执行
Phaser达到同等的效果
package com.java.test.phaser; import org.junit.Test; import java.util.Random; import java.util.concurrent.Phaser; /** * @Description: * @Author: Yourheart * @Create: 2023/1/4 23:45 */ public class PhaserTest { @Test public void test(){ Phaser phaser = new Phaser(5); for (int i = 0; i < 5; i++) { new Thread("线程-" + (i + 1)) { private final Random random = new Random(); @Override public void run() { System.out.println(getName() + " - 开始运行"); try { Thread.sleep(random.nextInt(1000)); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(getName() + " - 运行结束"); phaser.arrive(); } }.start(); } System.out.println("线程启动完毕"); phaser.awaitAdvance(phaser.getPhase()); //用Phaser替代CyclicBarrier考虑前面讲CyclicBarrier时,10个工程师去公司应聘的例子,也可以用Phaser实现,代码基本类似。 System.out.println("线程运行结束"); } }
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>
实现一起去食堂吃饭,吃饭完在一起去上课
单纯的使用测试注解,会没法执行完成,使用了定时任务实现
package com.java.test.phaser; import java.util.Random; import java.util.concurrent.Phaser; /** * @Description * @Author qiuxie * @Date 2023/1/5 22:08 */ public class PhaserThread extends Thread{ private final Phaser phaser; private final Random random = new Random(); public PhaserThread(String name, Phaser phaser) { super(name); this.phaser = phaser; } @Override public void run() { System.out.println(getName() + " - 起床"); slowly(); System.out.println(getName() + " - 洗漱完毕"); // 到达同步点,等待其他线程 phaser.arriveAndAwaitAdvance(); System.out.println(getName() + " - 食堂打完菜开始吃饭"); slowly(); System.out.println(getName() + " - 吃饭结束"); // 到达同步点,等待其他线程 phaser.arriveAndAwaitAdvance(); System.out.println(getName() + " - 准备出发去教室"); slowly(); System.out.println(getName() + " - 全部到达教室"); } private void slowly() { try { Thread.sleep(random.nextInt(6000)); } catch (InterruptedException e) { e.printStackTrace(); } } }
package com.java.test.phaser; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import java.util.concurrent.Phaser; /** * @Description * @Author qiuxie * @Date 2023/1/5 22:08 */ @Configuration //1.主要用于标记配置类,兼备Component的效果。 @EnableScheduling // 2.开启定时任务 public class PhaserThreadTest { /** * 当天的17点31分0秒执行 */ @Scheduled(cron = "0 31 17 * * *") @Async public void test(){ Phaser phaser = new Phaser(5); for (int i = 0; i <5 ; i++) { new PhaserThread("线程-"+(i+1),phaser).start(); } phaser.awaitAdvance(phaser.getPhase()); } }
特性1 动态调整线程个数
特性2 层次
一个大的任务细分为很多个小任务,最后将完成的结果汇总
state变量解析
Phaser没有基于AQS实现,但是具备AQS的核心特性
阻塞和唤醒