【多线程】happens-before规则

一、是什么
  • 1.规定了线程对共享变量的写操作对其它线程的读操作是可见的,是可见性与有序性的一套规则总结
  • 2.抛开happens-before规则,JMM并不能保证一个线程对共享变量的写,其它线程对该共享变量的读可见
二、有哪些
  • 1.线程解锁对象o之前对变量v的写,对后面对对象o加锁的其它线程对该变量v的读可见
  • 2.线程对volatitle变量的写,对接下来其它线程对该变量的读可见
  • 3.线程start前对变量的写,对该线程开始对该变量的读可见
  • 4.线程结束前对变量的写,对其它线程得知它结束后的读可见,例如线程2调用线程1的isAlive()或线程1的join()等待它结束
  • 5.线程1打断interrupt线程2之前对变量的写,对于其它线程得知线程t2被打断后对变量的读可见(通过线程2.interrupted或线程2.isInterrupted)
  • 6.对变量默认值(0,false,null)的写,其它线程对该变量的读可写
  • 7.具有传递性,如果x happens-before y,y happens-before z,那么x happens-before z,配合volatile的防指令重排
三、代码示例
3.1 线程解锁对象o之前对变量v的写,对后面对对象o加锁的其它线程对该变量v的读可见
3.1.1 不加synchronized锁
package com.learning.happens_before;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author wangyouhui
 * @Description
 **/
@Slf4j
public class Example1 {
    static int v;
    static Object o = new Object();
    public static void main(String[] args){
        new Thread(() -> {
//            synchronized (o) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                v = 10;
                log.info("线程1执行");
//            }
        }, "thread1").start();;

        new Thread(() -> {
//            synchronized (o) {
                while(v==0) {

                }
                log.info("线程2停止");
//            }
        }, "thread2").start();
        log.info("主线程执行完");
    }
}
3.1.2 加synchronized锁
package com.learning.happens_before;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author wangyouhui
 * @Description
 **/
@Slf4j
public class Example1 {
    static int v;
    static Object o = new Object();
    public static void main(String[] args){
        new Thread(() -> {
            synchronized (o) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                v = 10;
                log.info("线程1执行");
            }
        }, "thread1").start();;

        new Thread(() -> {
            synchronized (o) {
                while(v==0) {

                }
                log.info("线程2停止");
            }
        }, "thread2").start();
        log.info("主线程执行完");
    }
}

3.2 线程对volatitle变量的写,对接下来其它线程对该变量的读可见
3.2.1 不加volatitle
package com.learning.happens_before;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author wangyouhui
 * @Description volatile
 **/
@Slf4j
public class Example2 {
    static int v;
    public static void main(String[] args){
        new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            v = 10;
            log.info("线程1执行");
        }, "thread1").start();;

        new Thread(() -> {
            while(v==0) {

            }
            log.info("线程2停止");
        }, "thread2").start();
        log.info("主线程执行完");
    }
}

3.2.2 加volatitle
package com.learning.happens_before;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author wangyouhui
 * @Description volatile
 **/
@Slf4j
public class Example2 {
    volatile static int v;
    public static void main(String[] args){
        new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            v = 10;
            log.info("线程1执行");
        }, "thread1").start();;

        new Thread(() -> {
            while(v==0) {

            }
            log.info("线程2停止");
        }, "thread2").start();
        log.info("主线程执行完");
    }
}

3.3 线程start前对变量的写,对该线程开始后对该变量的读可见
package com.learning.happens_before;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author wangyouhui
 * @Description start线程之前对变量的写
 **/
@Slf4j
public class Example3 {
    static int x;

    public static void main(String[] args) {
        x = 10;
        new Thread(()->{
            log.info("线程打印:{}",x);
        }, "线程1").start();
    }
}
3.5 线程解锁对象o之前对变量v的写,对后面对对象o加锁的其它线程对该变量v的读可见
package com.learning.happens_before;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author wangyouhui
 * @Description interrupt
 **/
@Slf4j
public class Example3 {
    static int v;
    public static void main(String[] args){
        Thread t2 = new Thread(() -> {
            while(true){
                log.info("线程2打断前{}", v);
                if(Thread.currentThread().isInterrupted()){
                    log.info("线程2打断后{}", v);
                    break;
                }
            }
        }, "thread2");
        t2.start();

        new Thread(() -> {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            v = 10;
            t2.interrupt();
        }, "thread1").start();

        while(!t2.isInterrupted()){
            Thread.yield();
        }
        log.info("主线程{}", v);
        log.info("主线程执行完");
    }
}
3.6 对变量默认值(0,false,null)的写,其它线程对该变量的读可写
package com.learning.happens_before;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author wangyouhui
 * @Description 线程结束前对变量的写,对其它线程得知它结束后的读可见
 **/
@Slf4j
public class Example6 {
    static int v;

    public static void main(String[] args) {

        Thread thread1 = new Thread(()->{
            v = 10;
        }, "线程1");

        Thread thread2 = new Thread(()->{
            while(v == 0) {
                log.info("线程2打印:{}", v);
            }
        }, "线程2");
        thread2.start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread1.start();

        log.info("主线程打印:{}", v);
    }
}

3.7 如果x happens-before y,y happens-before z,那么x happens-before z
package com.learning.happens_before;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author wangyouhui
 * @Description
 **/
@Slf4j
public class Example7 {
    volatile static int v;
    static int y;

    public static void main(String[] args) {

        new Thread(()->{
            y = 20;
            v = 10;
        }, "线程1").start();

        new Thread(()->{
            // v = 10对线程2可见,那么y=10也对线程2可见,因为对v的写,使得v=10之前的操作都会同步到主存中
            log.info("线程2打印:{}", v);
        }, "线程2").start();

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王佑辉

老板,赏点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值