使用volatile简单实现happen-before功能
首先解释appen-before,其作用就是保证两个操作的顺序性,特别是多线程中,确保数据的准确性,对于执行顺序会有一定的要求
这里引入volatile手动设置
类似的synchronized也可以实现happen-before
测试代码
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 | package com.java.test.happer.before.volatiles; /** * @Description: * @Author: Yourheart * @Create: 2022/11/3 22:54 */ public class VolatileTest { int x = 0 ; volatile int y = 0 ; int a = 0 ; int b = 0 ; public void set() { a = 1 ; x = b; } public int get() { b = 1 ; y = a; return y; } @Override public String toString() { return "VolatileTest{" + "x=" + x + ", y=" + y + ", a=" + a + ", b=" + b + '}' ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.java.test.happer.before.volatiles; import lombok.extern.slf4j.Slf4j; /** * @Description: * @Author: Yourheart * @Create: 2022/11/3 23:24 */ @Slf4j public class ThreadDemoOne implements Runnable { private VolatileTest volatileTest; public ThreadDemoOne(VolatileTest volatileTest) { this .volatileTest = volatileTest; } @Override public void run() { this .volatileTest.set(); log.info( "【线程一】volatileTest:{}" ,volatileTest.toString()); } } |
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 | package com.java.test.happer.before.volatiles; import lombok.extern.slf4j.Slf4j; /** * @Description: * @Author: Yourheart * @Create: 2022/11/3 23:25 */ @Slf4j public class ThreadDemoTwo implements Runnable { private VolatileTest volatileTest; public ThreadDemoTwo(VolatileTest volatileTest) { this .volatileTest = volatileTest; } @Override public void run() { int i = this .volatileTest.get(); log.info( "获取到的y的数据:{}" ,i); log.info( "【线程二】volatileTest:{}" ,volatileTest.toString()); } } |
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 | package com.java.test.happer.before.volatiles; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * @Description: * @Author: Yourheart * @Create: 2022/11/3 23:28 */ public class ThreadPoolExecutorDemo { /** * 核心线程数 */ static int corePoolSize = 3 ; /** * 最大线程数 */ static int maximumPoolSize = 6 ; /** * 超过 corePoolSize 线程数量的线程最大空闲时间 */ static long keepAliveTime = 2 ; /** * 以秒为时间单位 */ static TimeUnit unit = TimeUnit.SECONDS; /** * 创建工作队列,用于存放提交的等待执行任务 */ static BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>( 2 ); static ThreadPoolExecutor threadPoolExecutor = null ; private static void initThreadPool() { threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, new ThreadPoolExecutor.AbortPolicy()); } public static void executed(Runnable runnable){ if (threadPoolExecutor== null ){ initThreadPool(); } threadPoolExecutor.execute(runnable); } } |
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 | package com.java.test.happer.before.volatiles; import lombok.extern.slf4j.Slf4j; import org.junit.Test; /** * @Description: * @Author: Yourheart * @Create: 2022/11/3 23:27 */ @Slf4j public class ThreadTest { @Test public void test(){ while ( true ){ try { Thread.sleep( 2000 ); } catch (InterruptedException e) { e.printStackTrace(); } VolatileTest volatileTest= new VolatileTest(); ThreadPoolExecutorDemo.executed( new ThreadDemoOne(volatileTest)); ThreadPoolExecutorDemo.executed( new ThreadDemoTwo(volatileTest)); } } } |
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 | <?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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <finalName>study</finalName> </build> </project> |
在单线程中,执行顺序即使有变化,最终结果不会错乱,但是多线程中就会出现数据错乱的问题,其实y是有返回0的情况,数据打印的时候出现过一次,后面都是y为1的情况
标签:
多线程
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!