当前位置:网站首页>Happens-before rules for threads
Happens-before rules for threads
2022-08-05 09:05:00 【Seven kingdoms of the world, I want ninety-nine】
线程之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 */
边栏推荐
- MySQL database error The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)
- DPU — 功能特性 — 安全系统的硬件卸载
- ts/js function pass parameter with function writing
- 为什么我推荐使用智能化async?
- 15.1.1、md—md的基础语法,快速的写文本备忘录
- 请问如果想往mysql里面写数据,直接用flink-connector-jdbc就可以吧,可是我在f
- 在colab里怎样读取google drive数据
- 【零基础玩转BLDC系列】无刷直流电机无位置传感器三段式启动法详细介绍及代码分享
- 使用HBuilder离线本地打包ipa教程
- XCODE12 在使用模拟器(SIMULATOR)时编译错误的解决方法
猜你喜欢

What is a good movie to watch on Qixi Festival?Crawl movie ratings and save to csv file

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

Concurrent CAS

【ASM】字节码操作 方法的初始化 Frame

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

全面讲解GET 和 POST请求的本质区别是什么?原来我一直理解错了

MySQL database error The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)

国际原子能机构总干事称乌克兰扎波罗热核电站安全形势堪忧

放大器OPA855的噪声计算实例

spark集群部署(第三弹)
随机推荐
sphinx匹配指定字段
Creo 9.0 基准特征:基准点
Xcode10的打包方式distribute app和启动项目报错以及Xcode 打包本地ipa包安装到手机上
16种香饭做法全攻略
全面讲解GET 和 POST请求的本质区别是什么?原来我一直理解错了
在colab里怎样读取google drive数据
Example of Noise Calculation for Amplifier OPA855
【LeetCode】623. Add a row to the binary tree
Why do I recommend using smart async?
thinkPHP5 realizes clicks (data increment/decrement)
Spark cluster deployment (third bullet)
my journal link
(转)[Json]net.sf.json 和org.json 的差别及用法
MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
Thinking and summary of the efficiency of IT R&D/development process specification
【ASM】字节码操作 方法的初始化 Frame
php fails to write data to mysql
这样写有问题吗?怎么在sql-client 是可以做到数据的同步的
吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(下)
leetcode refers to Offer 10- II. Frog jumping steps