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就是保证每次读数据都会从内存中读取,但只是保证多线程内共享资源的可见性。
可见性,只是保证取出来的数据是当前内存中放的数据,但无法保证数据一定写入正确。
6.可见性关键字(volidate)的更多相关文章
- 关键字volidate和transient(转)
Volatile修饰的成员变量在每次被线程访问时,都强迫从主内存中重读该成员变量的值.而且,当成员变量发生变化时,强迫线程将变化值回写到主内存.这样在任何时刻,两个不同的线程总是看到某个成员变量的同一 ...
- scala教程之:可见性规则
文章目录 public Protected private scoped private 和 scoped protected 和java很类似,scala也有自己的可见性规则,不同的是scala只有 ...
- Volatile关键字和ThreadLocal变量的简单使用
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11812459.html package thread; /** * volatile关键字和T ...
- 《Java虚拟机并发编程》学习笔记
对<Java虚拟机并发编程>这本书真的是相见恨晚.以前对并发编程只是懂个皮毛,这本书让我对并发编程有了一个全新的认识.所以把书上的知识点做下笔记,以便以后复习使用. 并发与并行 仔细说来, ...
- 在Xcode中使用Clang Format
Xcode中的Re-Indent,顾名思义,只是一个调整缩进的功能,完全依赖它来进行代码格式化显然不够用.我们使用了一个叫做ClangFormat-Xcode的插件,配合Re-Indent一起来做代码 ...
- 在Visual Studio中使用AStyle
最近在做一个C++项目,我们使用了一个叫做AStyle的插件来做代码格式化. 下载方式1:通过Visual Studio下载 启动Visual Studio,以下简称VS: 英文版VS:VS主菜单 & ...
- PHP面向对象编程学习之对象基础
php虽然是一门学习起来非常简单的语言,但是这门语言也包含了对面向对象编程的支持.尤其是随着php5的发布,php对面向对象的支持有了很大的进步.最近学习了一下php的面向对象编程,不禁感慨,面向对象 ...
- DELPHI学习---类和对象(五篇)
Classes and objects(类和对象) 类(或者类类型)定义了一个结构,它包括字段(也称为域).方法和属性:类的实例叫做对象:类的字段.方法和属性被称为它的部件(components)或成 ...
- 作为一个新手的Oracle(DBA)学习笔记【转】
一.Oracle的使用 1).启动 *DQL:数据查询语言 *DML:数据操作语言 *DDL:数据定义语言 DCL:数据控制语言 TPL:事务处理语言 CCL:指针控制语言 1.登录 Win+R—cm ...
随机推荐
- 从零开始的全栈工程师——html篇1.7
position定位与表单 一.position 1.Position细说 Position:relative; Left:100px; Top:100px; Position:absolute; L ...
- 工作中常用的sql语句以及知识整理
一.常用的sql语句 1.建表语句 create table tabname(colname1 type1 [not null][primary key], colname2 type2,...) 根 ...
- C语言--清理getchar缓存
getchar()采用了缓冲区,而getch()才是立即获取,所以要想再用getchar()获取正确的值必须先清空缓冲区,如果是windows操作系统,用fflush(stdin)函数或rewind( ...
- 将ReportingService 2008配置为匿名访问 - 转载
众所周知在ReportingService 2008中微软取消了匿名访问,我们再也不能像2005时那样靠修改IIS来实现匿名访问了.但在微软MSDN BLOG中还是提供了以下这种设置匿名访问的方式. ...
- 03_Zookeeper基本数据模型及基本命令操作
[Zookeeper基本数据模型及注意点] * zk的数据模型可以类比为Linux的文件目录,是一种树状结构,如:/dubbo/com.service.DemoService/provider.... ...
- VC添加文件到工程出错问题--FileTool.dll
原文:http://blog.csdn.net/bingdianlanxin/article/details/45112737 在我们的软件开发中,经常需要导入其他文件到我们的工程. 一般,我们会选择 ...
- java反射机制的简单介绍
参考博客: https://blog.csdn.net/mlc1218559742/article/details/52754310 先给出反射机制中常用的几个方法: Class.forName (& ...
- windows c++ 修改用户的文件夹操作权限
一般Windows下的系统文件(夹)只让受限帐户读取而不让写入和修改.如果要开启写操作权限就需要手动修改文件(夹)的用户帐户安全权限(这操作当然要在管理员帐户下执行).以下用程序封装了一下该操作: # ...
- SAP CRM系统订单模型的设计与实现
SAP成都研究院的一个部门领导让我给他的团队做一个SAP CRM One Order框架的培训,这是我准备的培训内容. 在Jerry之前的文章 基于SAP Kyma的订单编排增强介绍,我表达了自己对S ...
- 在自己机器上实现apache的多域名
自己机器上有3个目录,分别是/var/www/html/ /var/www/solaris/ ...