线程中join简单使用
暂停线程的执行,直到调用该方法的线程执行结束为止。可以使用join等待另一个thread对象结束
在多个线程中,我们需要保证某个线程执行之前先把另外的线程先执行了,这里我们可以使用join方法,下面给出案例效果图
代码部分
package com.java.thread.test; /** * @Description: * @Author: qiuxie * @Create: 2023/8/9 21:49 */ public class JoinThread { public static class Goddess implements Runnable{ private Thread thread; public void setThread(Thread thread) { this.thread = thread; } public Goddess(Thread thread) { this.thread = thread; } public Goddess() { } @Override public void run() { System.out.println("女神开始排队打饭"); try { if (thread!=null){ thread.join(); } } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("女神打饭完成...."); } } public static class GoddessBoyfriend implements Runnable{ @Override public void run() { System.out.println("女神男朋友开始打饭"); System.out.println("女神男朋友打饭完成"); } } public static void main(String[] args) throws InterruptedException { GoddessBoyfriend boyfriend = new GoddessBoyfriend(); Thread gbf = new Thread(boyfriend); Goddess goddess = new Goddess(gbf); Thread thread = new Thread(goddess); thread.start(); gbf.start(); System.out.println("备胎开始排队打饭...."); thread.join(); 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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.java</groupId> <artifactId>web-study</artifactId> <version>0.0.1-SNAPSHOT</version> <name>web-study</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.28</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--ThreadFactoryBuilder的依赖包,多线程使用--> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>30.1-jre</version> </dependency> <!--引入junit单元测试依赖--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--lombok依赖--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> </dependency> <!--添加fastjson依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <!--<version>1.2.7</version>--> <version>1.2.70</version> </dependency> <dependency> <groupId>co.paralleluniverse</groupId> <artifactId>quasar-core</artifactId> <version>0.7.9</version> </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> </build> </project>