可见性关键字(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. 如何将一个SpringBoot简便地打成一个war包(转)

    为什么要把SpringBoot打成war包 正常情况下SpringBoot项目是以jar包的形式,通过命令行: java -jar demo.jar 来运行的,并且SpringBoot是内嵌Tomca ...

  2. jQuery extend方法详解

    先说个概念的东西: jQuery为开发插件提拱了两个方法,分别是: $.fn.extend(item):为每一个实例添加一个实例方法item.($("#btn1") 会生成一个 j ...

  3. js 密码 正则表达式

    1. 代码 function checkPassword(str){ var reg1 = /[!@#$%^&*()_?<>{}]{1}/; var reg2 = /([a-zA- ...

  4. js权威指南学习笔记(三)语句

    1.声明语句 如果用var声明的变量没有初始化,那么这个变量的值会被初始化为undefined. 函数声明语句的语法如下:       4 4           1 console.log(func ...

  5. Express (Routing、Middleware、托管静态文件、view engine 等等)

    1. Express 简介 Express 是基于 Node.js 平台,快速.开放.极简的 web 开发框架,它提供一系列强大的特性,帮助你创建各种 Web 和移动设备应用. Express 不对 ...

  6. Android热修复 Dex注入实现静默消灭bug

    当app上线后发现紧急bug,如果重新发布版本周期比较长,并且对用户体验不好,此时热修复就派上用场了.热修复就是为紧急bug而生,能够快速修复bug,并且用户无感知.针对热修复,阿里系先后推出AndF ...

  7. html select 操作备份

    select动态添加option问题 JS操作:(未验证) 代码 var selid = document.getElementById("sltid"); for(var i=0 ...

  8. Java不带.classpath的svn项目下载,转成到eclipse中

    .classpath是Eclipse的工程文件,别人没有将工程的信息传到SVN库中,就检查不出.classpath文件,识别不了项目结构. 这种做法也是比较提倡的方法.SVN上只要有项目的源码信息就可 ...

  9. yii2.0中url重写实现方法

    在yii框架里有前台和后台页面,举例前台url重写. 控制器与路由 控制器以Controller作为后缀,继承自yii\web\Controller; 动作以action作为前缀,public访问修饰 ...

  10. 深入编解码:ASCII,UNICODE,UTF8等

    ASCII ASCII = American Standard Code for Information Interchange(美国信息交换标准码) 美国最先有了计算机技术,计算机里面只有01,也就 ...