JAVA多线程可见性

  为了更清楚的了解可见性,这里先介绍一下jmm。jmm并非是一个组件或者程序,他是抽象计算机内存模型的一组规则、规范,定义了变量访问内存的方式。

 

如图:

  jmm的每个线程有独立的工作内存,他们的工作方式是从主内存将变量读取到自己的工作内存,然后在工作内存中进行逻辑或者自述运算再把变量写回到主内存中。正常情况下各线程的工作内存之间是相互隔离的、不可见的。

有这样一个场景,两个小孩子玩游戏,游戏内容是A小孩跳绳,B小孩叫停。
代码示例1:
public class Game {
private volatile boolean stop = false;

public void stop() {
this.stop = true;
String name = Thread.currentThread().getName();
System.out.println(name + "小孩叫停: ");
}

public void ropeSkipping() {
String name = Thread.currentThread().getName();
int i = 0;
for (;!this.stop;i++) {

}
System.out.println(name + "小孩跳了: " + i + "次");
}

public class Play {
public static void main(String[] args) {
Game game = new Game();
Thread childB = new Thread(()->{
game.stop();
},"childB");

Thread childA = new Thread(()->{
game.ropeSkipping();
},"childA");
//让A先跳
childA.start();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
childB.start();
}
}

如果没有volatile关键字,线程childB执行了stop方法线程childA也是不会停下来的。volatile的作用保证了变量在不同线程间的可见性。

代码示例2:
public class Game {
private boolean stop = false;

public void stop() {
this.stop = true;
String name = Thread.currentThread().getName();
System.out.println(name + "小孩叫停: ");
}

public void ropeSkipping() {
String name = Thread.currentThread().getName();
int i = 0;
for (;!this.stop;i++) {

System.out.println(name + "小孩跳了: " + i + "次");

}
}

示例2去掉了volatile关键字,把System.out.println输出语句放入了循环中,线程childB执行了stop方法线程childA会停下来,因为System.out.println里面有包含了synchronized关键字,synchronized也有让线程间可见性的作用。

除此之外,final关键字也能让变量(常量)线程可见,就不一一举例了。


 

posted @ 2019-08-20 22:16  书山畅游  Views(3498)  Comments(0Edit  收藏  举报