可见性关键字(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就是保证每次读数据都会从内存中读取,但只是保证多线程内共享资源的可见性。

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

6.可见性关键字(volidate)的更多相关文章

  1. 关键字volidate和transient(转)

    Volatile修饰的成员变量在每次被线程访问时,都强迫从主内存中重读该成员变量的值.而且,当成员变量发生变化时,强迫线程将变化值回写到主内存.这样在任何时刻,两个不同的线程总是看到某个成员变量的同一 ...

  2. scala教程之:可见性规则

    文章目录 public Protected private scoped private 和 scoped protected 和java很类似,scala也有自己的可见性规则,不同的是scala只有 ...

  3. Volatile关键字和ThreadLocal变量的简单使用

    原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11812459.html package thread; /** * volatile关键字和T ...

  4. 《Java虚拟机并发编程》学习笔记

    对<Java虚拟机并发编程>这本书真的是相见恨晚.以前对并发编程只是懂个皮毛,这本书让我对并发编程有了一个全新的认识.所以把书上的知识点做下笔记,以便以后复习使用. 并发与并行 仔细说来, ...

  5. 在Xcode中使用Clang Format

    Xcode中的Re-Indent,顾名思义,只是一个调整缩进的功能,完全依赖它来进行代码格式化显然不够用.我们使用了一个叫做ClangFormat-Xcode的插件,配合Re-Indent一起来做代码 ...

  6. 在Visual Studio中使用AStyle

    最近在做一个C++项目,我们使用了一个叫做AStyle的插件来做代码格式化. 下载方式1:通过Visual Studio下载 启动Visual Studio,以下简称VS: 英文版VS:VS主菜单 & ...

  7. PHP面向对象编程学习之对象基础

    php虽然是一门学习起来非常简单的语言,但是这门语言也包含了对面向对象编程的支持.尤其是随着php5的发布,php对面向对象的支持有了很大的进步.最近学习了一下php的面向对象编程,不禁感慨,面向对象 ...

  8. DELPHI学习---类和对象(五篇)

    Classes and objects(类和对象) 类(或者类类型)定义了一个结构,它包括字段(也称为域).方法和属性:类的实例叫做对象:类的字段.方法和属性被称为它的部件(components)或成 ...

  9. 作为一个新手的Oracle(DBA)学习笔记【转】

    一.Oracle的使用 1).启动 *DQL:数据查询语言 *DML:数据操作语言 *DDL:数据定义语言 DCL:数据控制语言 TPL:事务处理语言 CCL:指针控制语言 1.登录 Win+R—cm ...

随机推荐

  1. 学习笔记2_Day09_servlet的细节

    Servlet细节 l  不要在Servlet中创建成员!创建局部变量即可! l  可以创建无状态成员! l  可以创建有状态的成员,但状态必须为只读的! 1 Servlet与线程安全 因为一个类型的 ...

  2. The method setItems(String) in the type ForTokensTag is not applicable for the arguments (Object)

    1. 问题 看到这个错误以为是貌似jsp页面有误,c:forTokens标签用错了?? An error occurred at line: in the jsp file: /WEB-INF/pag ...

  3. 汉诺塔matlab实现

    Matlab的递归调用,好久不用matlab了,练练手.   global handCount; handCount = 1; huuotsun(1, 2, 3, 3)     function hu ...

  4. Android输入法架构学习总结

    此文为本人学习输入法之后所做的一个总结报告.与大家分享. 安卓输入法框架(Input Method Framework)IMF 一.输入法框架简介 自Android平台1.5版本以后,Google开放 ...

  5. 【2014-08-23】Beyong Coding

        be a product engineer,not be a engineer 选择比努力更重要   just code it,code it ,until you make it 缺的不是i ...

  6. ie6 浏览器的bug

    1.IE6不支持连续类的交集选择器 1 #box.box.box1{ 2             width: 200px; 3             height: 200px; 4       ...

  7. Oracle中用户和方案的区别

    从定义中我们可以看出方案(Schema)为数据库对象的集合,为了区分各个集合,我们需要给这个集合起个名字,这些名字就是我们在企业管理器的方案下看到的许多类似用户名的节点,这些类似用户名的节点其实就是一 ...

  8. jenkins start

    author:alex.wang date:2017.05.25 system:centos7.2 ip:192.168.1.46 download: wget http://mirrors.jenk ...

  9. Microsoft Office ->> 完整卸载Office 2007

    今天用GHOST安装了Windows 8.1,结果发现预装了Office 2007,而且这个GHOST系统的Office 2007还不是很正规的安装手法安装的.它没有在注册表中注册.在打开控制面板后发 ...

  10. 正则去除html标签属性保留指定标签

    /// <summary> /// 去除标签里面的属性保留IMG标签属性 /// </summary> /// <param name="strText&quo ...