网站保留密码 怎么做,眼查看网站开发语言,眼镜网站怎么做,外链代发工具禁用CPU 缓存
告诉编译器#xff0c;对这个变量的读写#xff0c;不能使用 CPU 缓存#xff0c;必须从内存中读取或者写入
/*** TODO 在此写上类的相关说明.br* author gqlttbr* version 1.0.0 2020年4月8日br* see * since JDK 1.5.0*/
public c…禁用CPU 缓存
告诉编译器对这个变量的读写不能使用 CPU 缓存必须从内存中读取或者写入
/*** TODO 在此写上类的相关说明.br* author gqlttbr* version 1.0.0 2020年4月8日br* see * since JDK 1.5.0*/
public class VolatileExample {int x 0;volatile boolean b false;public void writer() {x 42;b true;}public void reader() {if(b) {System.out.println(x x);}}/*** param args*/public static void main(String[] args) {VolatileExample example new VolatileExample();Thread th1 new Thread(()-{CommonUtil.sleep(100);example.writer();});Thread th2 new Thread(()-{CommonUtil.sleep(300);example.reader();});th1.start();th2.start();try {th1.join();th2.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} 1、根据规则1x42 Happens-Before vtrue 2、根据规则2写变量vtrue Happens-Before 读变量 vtrue 3、根据规则3x42 Happens-Before 读变量 vtrue