线程中join简单使用
暂停线程的执行,直到调用该方法的线程执行结束为止。可以使用join等待另一个thread对象结束
在多个线程中,我们需要保证某个线程执行之前先把另外的线程先执行了,这里我们可以使用join方法,下面给出案例效果图
代码部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 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文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | <?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> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异