java volidate_6.可见性关键字(volidate)

可见性关键字(volidate):

如果对java内存模型了解较清楚的话,我们知道每个线程都会被分配一个线程栈。

线程栈里存的是对象的引用,但当前cache缓存机制,可能会把数据拷贝。

就是,命中缓存,去数据是从cache中获取,而不是从本地内存读取。

不加关键字实例:

package com.xm.thread.t_19_01_27;

import java.util.concurrent.TimeUnit;

public class VolatileDemo implements Runnable{

Boolean state = false;

volatile int count = 0;

@Override

public void run() {

if(state==true || count<10) {

count ++;

System.out.println("state="+state+";count="+count);

} else {

System.out.println("state="+state+";count="+count);

}

}

public static void main(String[] args) throws InterruptedException {

VolatileDemo demo = new VolatileDemo();

for(int i=0;i<100;i++) {

new Thread(demo).start();

}

TimeUnit.MILLISECONDS.sleep(10);

demo.state = true;

TimeUnit.MILLISECONDS.sleep(10);

demo.state = false;

TimeUnit.MILLISECONDS.sleep(10);

demo.state = true;

}

}

运行结果:

state=false;count=2

state=false;count=2

state=false;count=3

state=false;count=2

state=false;count=4

state=false;count=5

state=false;count=6

state=false;count=7

state=false;count=8

state=false;count=9

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=true;count=19

state=true;count=18

state=true;count=17

state=true;count=16

state=true;count=15

state=true;count=15

state=true;count=15

state=true;count=13

state=true;count=12

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

加关键字实例:

package com.xm.thread.t_19_01_27;

import java.util.concurrent.TimeUnit;

public class VolatileDemo implements Runnable{

volatile Boolean state = false;

volatile int count = 0;

@Override

public void run() {

if(state==true || count<10) {

count ++;

System.out.println("state="+state+";count="+count);

} else {

System.out.println("state="+state+";count="+count);

}

}

public static void main(String[] args) throws InterruptedException {

VolatileDemo demo = new VolatileDemo();

for(int i=0;i<100;i++) {

new Thread(demo).start();

}

TimeUnit.MILLISECONDS.sleep(10);

demo.state = true;

TimeUnit.MILLISECONDS.sleep(10);

demo.state = false;

}

}

运行结果:

state=false;count=1

state=false;count=2

state=false;count=3

state=false;count=4

state=false;count=5

state=false;count=6

state=false;count=7

state=false;count=8

state=false;count=9

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=true;count=12

state=false;count=10

state=true;count=14

state=true;count=17

state=true;count=18

state=true;count=21

state=true;count=25

state=true;count=28

state=false;count=10

state=true;count=31

state=true;count=32

state=true;count=33

state=true;count=35

state=true;count=29

state=true;count=28

state=true;count=26

state=true;count=24

state=true;count=23

state=true;count=22

state=true;count=20

state=true;count=19

state=true;count=17

state=true;count=15

state=true;count=13

state=true;count=11

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=false;count=10

state=true;count=42

state=true;count=41

state=true;count=41

state=true;count=40

state=true;count=39

state=true;count=39

state=true;count=39

state=true;count=34

state=true;count=31

结果分析:

volidate就是保证每次读数据都会从内存中读取,但只是保证多线程内共享资源的可见性。

可见性,只是保证取出来的数据是当前内存中放的数据,但无法保证数据一定写入正确。

原文:https://www.cnblogs.com/TimerHotel/p/thread06.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值