当前位置:网站首页>线程之Happens-before规则
线程之Happens-before规则
2022-08-05 08:52:00 【七国的天下,我要九十九】
线程之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 */
边栏推荐
- [Structure internal power practice] Structure memory alignment (1)
- 让硬盘更快,让系统更稳定
- ps怎么替换颜色,自学ps软件photoshop2022,ps一张图片的一种颜色全部替换成另外一种颜色
- sql server中 两表查询 平均数 分组
- Fiddler tool explanation
- 这样写有问题吗?怎么在sql-client 是可以做到数据的同步的
- How to replace colors in ps, self-study ps software photoshop2022, replace one color of a picture in ps with another color
- Chapter 12 Bayesian Networks
- CROS and JSONP configuration
- Linux导出数据库数据到硬盘
猜你喜欢

Thinking and summary of the efficiency of IT R&D/development process specification

施一公:科学需要想象,想象来自阅读

Pagoda measurement - building small and medium-sized homestay hotel management source code

php fails to write data to mysql

嵌入式实操----基于RT1170 移植memtester做SDRAM测试(二十五)

使用HBuilder离线本地打包ipa教程

MySQL 数据库 报错 The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)

Redis cache and existing problems--cache penetration, cache avalanche, cache breakdown and solutions

Embedded practice ---- based on RT1170 transplant memtester to do SDRAM test (25)

How to make pictures clear in ps, self-study ps software photoshop2022, simple and fast use ps to make photos clearer and more textured
随机推荐
RedisTemplate: error template not initialized; call afterPropertiesSet() before using it
Three solutions to solve cross-domain in egg framework
Pagoda measurement - building small and medium-sized homestay hotel management source code
Linux导出数据库数据到硬盘
love is a sad song
The color of life divine
Detailed explanation of DNS query principle
接口全周期的生产力利器Apifox
XCODE12 在使用模拟器(SIMULATOR)时编译错误的解决方法
ts/js function pass parameter with function writing
MySQL database error The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)
CROS和JSONP配置
Creo 9.0 基准特征:基准平面
[Repost] Marry a man must marry a man whose salary is at least 3571.4 yuan higher than yours
今天是元宵节~~
【Excel实战】--图表联动demo_001
Data source object management Druid and c3p0
k-nearest neighbor fault monitoring based on multi-block information extraction and Mahalanobis distance
Luogu: P2574 XOR的艺术 [线段树]
嵌入式实操----基于RT1170 移植memtester做SDRAM测试(二十五)