首页 > 其他分享 >使用volatile简单实现happen-before功能

使用volatile简单实现happen-before功能

时间:2022-11-05 00:55:44浏览次数:77  
标签:java boot volatileTest volatile org happen public before

首先解释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

相关文章

  • volatile
     加关键字volatileJava语言包含两种内在的同步机制:同步块(或方法)和volatile变量,相比于synchronized(synchronized通常称为重量级锁),volatile更轻量级,因为它不会引起线程......
  • C 语言 volatile 关键字
    《CPrimerPuls》是这样解释关键字的:关键字是C语言的词汇,由于编译器不具备真正的智能,所以你必须用编译器能理解的术语表示你的意图。volatile关键字告诉编译器该变量是......
  • 关于Java的volatile关键字、内存屏障的思考
    内存屏障就是一种屏障指令,在X86架构中,指的是加了“lock前缀”的汇编指令在执行时会让CPU或编译器在对内存进行操作的时候,严格按照一定的顺序来执行。也就是说......
  • 既然CPU有缓存一致性协议(MESI),为什么JMM还需要volatile关键字?
    缓存一致性协议是保证“硬件CPU的cache”和“硬件内存”之间数据的一致性,更详细说也就是缓存一致性协议是保证“物理计算机中的硬件CPUcache”和“物理计算机中的......
  • Lifetime improvement through adaptive reconfiguration for nonvolatile FPGAs
    LifetimeimprovementthroughadaptivereconfigurationfornonvolatileFPGAsSRAM的FPGA有漏电和容量的限制,使用NVM替换SRAM是一个有效的方式,但是NVM有寿命问题。BRAM......
  • volatile
    volatile意为易变加在变量前,告诉编译器这是一个值可能会发生变化的变量,不要进行优化优化做法是,由于编译器发现两次从i读数据的代码之间的代码没有对i进行过操作,它会自动......
  • WARN: Findbugs needs sources to be compiled. Please build project before executi
    ERROR:ErrorduringSonarScannerexecutionjava.lang.IllegalStateException:CannotexecuteFindbugs atorg.sonar.plugins.findbugs.FindbugsExecutor.execute(Fi......
  • Spring AOP @Before @Around @After 等 advice 的执行顺序
    最近在写项目时用到了AOP的advice来实现通知,后来了解到它在权限控制及其他方面也有很好的利用价值,所以整理了一份AOP各种通知的执行顺序在一个方法只被一个aspect类拦截时,as......
  • 深度剖析Java的volatile实现原理,再也不怕面试官问了
    上篇文章我们讲了synchronized的用法和实现原理,我们总爱说synchronized是重量级锁,volatile是轻量级锁。为什么volatile是轻量级锁,体现在哪些方面?以及volatile的作用和实现......
  • 既然CPU有缓存一致性协议(MESI),为什么JMM还需要volatile关键字?
    ​​既然CPU有缓存一致性协议(MESI),为什么JMM还需要volatile关键字?​​​​MESI缓存一致性协议在哪里以及如何实现?​​​​Intel®64andIA-32ArchitecturesDeveloper’s......