目录
-
-
-
- 一、是什么
- 二、有哪些
- 三、代码示例
-
- 3.1 线程解锁对象o之前对变量v的写,对后面对对象o加锁的其它线程对该变量v的读可见
-
- 3.1.1 不加synchronized锁
- 3.1.2 加synchronized锁
- 3.2 线程对volatitle变量的写,对接下来其它线程对该变量的读可见
-
- 3.2.1 不加volatitle
- 3.2.2 加volatitle
- 3.3 线程start前对变量的写,对该线程开始后对该变量的读可见
- 3.5 线程解锁对象o之前对变量v的写,对后面对对象o加锁的其它线程对该变量v的读可见
- 3.6 对变量默认值(0,false,null)的写,其它线程对该变量的读可写
- 3.7 如果x happens-before y,y happens-before z,那么x happens-before z
一、是什么
- 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();
}
}