可见性关键字(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. Tomcat服务器配置https认证(使用keytool生成证书)

    一.证书生成 1.生成服务器证书 (1)打开打开命令控制台,进入jdk的bin目录 cd D:\Program Files\jdk1.6.0_45\bin (2)keytool为Tomcat生成证书( ...

  2. C 堆内存管理

    在Win32 程序中每个进程都占有4GB的虚拟地址空间,这4G的地址空间内部又被分为代码段,全局变量段堆段和栈段,栈内存由函数使用,用来存储函数内部的局部变量,而堆是由程序员自己申请与释放的,系统在管 ...

  3. bzoj2119 [ZJOI2010]base基站选址

    传送门 n年前的考试题,今天才填上…… 听说你们会决策单调性+主席树?然而我多年不写决策单调性,懒得写了……于是就写了一发线段树. 其实线段树应该不难想,毕竟转移是分层转移,并且这个题的转移函数可以快 ...

  4. numpy用法介绍-未完待续

    简介 NumPy(Numerical Python简称) 是高性能科学计算和数据分析的基础包 为什么使用? 标准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元 ...

  5. hdu 2063 过山车(模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 过山车 Time Limit: 1000/1000 MS (Java/Others)    Me ...

  6. C++的字符串分割函数

    原文: C++的字符串没有分割函数,因此需要自己写方便使用.而受到开发工具的影响,有很多用起来比较麻烦啦,下面这个比较不错奥. 用STL进行字符串的分割 涉及到string类的两个函数find和sub ...

  7. 多线程 读写锁SRWLock

    在<秒杀多线程第十一篇读者写者问题>文章中我们使用事件和一个记录读者个数的变量来解决读者写者问题.问题虽然得到了解决,但代码有点复杂.本篇将介绍一种新方法——读写锁SRWLock来解决这一 ...

  8. 微信公众号 报token验证失败

    最近两天一直碰到token验证失败的问题,如下图所示 然后上网疯狂的搜索答案,碰到的人还真不少,原因大概有这几个: 1)服务器URL未实名认证 2)token重名(基本不可能) 3)服务器部署的项目 ...

  9. .net CombinedGeometry的合并模式

    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="h ...

  10. Java 调用 Rest api 设置经典 Linux 虚拟机的实例启停

    现象描述 用户可以通过 Rest API 设置经典 Linux 虚拟机实例的启停.在调用该 API 时需要通过 Azure Active Directory(下文简称 AAD) 获取 Token,但是 ...