首先解释appen-before,其作用就是保证两个操作的顺序性,特别是多线程中,确保数据的准确性,对于执行顺序会有一定的要求
这里引入volatile手动设置
类似的synchronized也可以实现happen-before
测试代码
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 + '}'; } }
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()); } }
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()); } }
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); } }
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文件
<?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>
在单线程中,执行顺序即使有变化,最终结果不会错乱,但是多线程中就会出现数据错乱的问题
标签:java,boot,volatileTest,volatile,org,happen,public,before From: https://www.cnblogs.com/q202105271618/p/16856219.html