温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java线程之Happens before规则是什么

发布时间:2022-08-03 16:08:52 来源:亿速云 阅读:113 作者:iii 栏目:开发技术

本篇内容介绍了“java线程之Happens before规则是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

正文

happens-before 规定了对共享变量的写操作对其它线程的读操作可见,它是可见性与有序性的一套规则总结,抛开以下 happens-before 规则,JMM 并不能保证一个线程对共享变量的写,对于其它线程对该共享变量的读可见.

案例1

线程解锁 m 之前对变量的写,对于接下来对 m 加锁的其它线程对该变量的读可见

    static int x;
    static Object m = new Object();

    new Thread(()->{
         synchronized(m) {
         x = 10;
         }
    },"t1").start();

    new Thread(()->{
         synchronized(m) {
         System.out.println(x);
         }
    },"t2").start();
/*
运行结果:
10
*/

案例2

线程对 volatile 变量的写,对接下来其它线程对该变量的读可见

    volatile static int x;
    new Thread(()->{
     x = 10;
    },"t1").start();

    new Thread(()->{
     System.out.println(x);
    },"t2").start();
/*
运行结果:
10
*/

案例3

线程 start 前对变量的写,对该线程开始后对该变量的读可见

static int x;
x = 10;
new Thread(()->{
 System.out.println(x);
},"t2").start();
/*
运行结果:
10
*/

案例4

线程结束前对变量的写,对其它线程得知它结束后的读可见(比如其它线程调用 t1.isAlive() 或 t1.join()等待 它结束)

static int x;
Thread t1 = new Thread(()->{
 x = 10;
},"t1");
t1.start();
t1.join();
System.out.println(x);
/*
运行结果:
10
*/

案例5

线程 t1 打断 t2(interrupt)前对变量的写,对于其他线程得知 t2 被打断后对变量的读可见(通过 t2.interrupted 或 t2.isInterrupted)

static int x;
public static void main(String[] args) {
    
     Thread t2 = new Thread(()->{
         while(true) {
             if(Thread.currentThread().isInterrupted()) {
             System.out.println(x);
             break;
             }
         }
     },"t2");
     t2.start();
    
     new Thread(()->{
         sleep(1);
         x = 10;
         t2.interrupt();
     },"t1").start();
     while(!t2.isInterrupted()) {
         Thread.yield();
     }
     System.out.println(x);
}
/*
运行结果:
10
*/

案例6

对变量默认值(0,false,null)的写,对其它线程对该变量的读可见

    static int a;
    public static void main(String[] args) {
        new Thread(()->{
            System.out.println(a);
        }).start();

    }
/*
运行结果:
0
*/

案例7

具有传递性,如果 x hb-> y 并且 y hb-> z 那么有 x hb-> z ,配合 volatile 的防指令重排,有下面的例子

volatile static int x;
static int y;
new Thread(()->{ 
 y = 10;
 x = 20;
},"t1").start();
new Thread(()->{
 // x=20 对 t2 可见, 同时 y=10 也对 t2 可见
 System.out.println(x); 
},"t2").start();
/*
运行结果:
20
*/

“java线程之Happens before规则是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI