volatile可见性

代码1:不使用volatile

/**volatile 可见性
* @author 雪辞
*
*/
public class TestVisibility {

private  boolean initFlag=false;
public void refresh(){
          this.initFlag=true;
          System.out.println("当前线程"+Thread.currentThread().getName()+"修改了这个变量");

}
public void load(){
          while(!initFlag){

}
          System.out.println("当前线程"+Thread.currentThread().getName()+"知道了这个变量发生了改变");
}
public static void main(String[] args) {
          TestVisibility a=new TestVisibility();
          Thread thread1=new Thread(()->a.load(),"thread1");
          Thread thread2=new Thread(()->a.refresh(),"thread2");
          thread1.start();
          //必须让load方法先去拿主内存的值放入线程的工作内存中,其他线程再去修改主内存的值
          try {
               Thread.sleep(2000);
          } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
          thread2.start();

     }
}

结果:

 

 说明:

          线程1的load方法一直在检测,但是检测不到initFlag修改了吗?

          这就需要理解线程的工作内容和主内存关系了。后面继续更新!

 

代码2:加入volatile
/**volatile 可见性
* @author 雪辞
*
*/
public class TestVisibility {

//加入volatile
private volatile boolean initFlag=false;
public void refresh(){
          this.initFlag=true;
          System.out.println("当前线程"+Thread.currentThread().getName()+"修改了这个变量");

}
public void load(){
          while(!initFlag){

}
          System.out.println("当前线程"+Thread.currentThread().getName()+"知道了这个变量发生了改变");
}
public static void main(String[] args) {
          TestVisibility a=new TestVisibility();
          Thread thread1=new Thread(()->a.load(),"thread1");
          Thread thread2=new Thread(()->a.refresh(),"thread2");
          thread1.start();
          //必须让load方法先去拿主内存的值放入线程的工作内存中,其他线程再去修改主内存的值
          try {
               Thread.sleep(2000);
          } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
          thread2.start();

     }
}

结果:

 

 说明:

    加入volatile就可以检测到initFlag被修改,可见性已解决。

posted @ 2020-04-22 13:41  雪辞妹  阅读(139)  评论(0)    收藏  举报