Java中volatile关键字原义是“不稳定、变化”的意思,那么在java语言中,volatile是什么意思呢?
volatile,在Java中用来修饰变量,那是为什么呢?volatile是告诉vm,这个变量不能被私自占有使用,下面就解释一下为什么这么说吧!
大家都知道,在java中,除了long和double的操作,对基本类型的操作是原子性的,那还为什么使用valotile关键字呢,因为随着jvm的优化和成熟,为了提高多线程的效率,允许每个线程在工作内存保存变量的拷贝进行操作,这就很容易出现数据不一致,所以volatile就强制是多核或多线程必须从主存中读写变量,保证变量的一致性。
由于比起synchronized,volatile关键字的覆盖范伟更小,只是修饰的变量,所以效率就比前者高,但是,它是存在问题的,不能保证“线程写的合理”,解释如下:
写的不合理,是可能存在竞争条件,而出现操作与设想结果不同。
假如,有100个线程正在对一个i变量惊醒i++操作,那么结果一定是100么,可能<100的,比如一个线程a读到的是i=3,几乎同时(a没有修改i),另一个线程也读到3,那么结果,两次相加就得到了i=4,而不是5,详细看看操作系统同步就知道了。
怎么处理呢?
1.使用synchronized关键字,但是这样就失去了volatile的意义。
2.使用CPU原语实现非阻塞算法。
http://blog.csdn.net/weizhaozhe/archive/2009/02/21/3919161.aspx
http://space.itpub.net/111631/viewspace-610344
分享到:
相关推荐
violate java_Java 之 volatile 超级详解
2.10 Intentionally or unintentionally violate any applicable local, state, national or international law, including securities exchange and any regulations requirements, procedures or policies in ...
shall inter-operate seamlessly together.Any unauthorized use of this specification may violate copyright laws, trademark laws, and communications regulations and statutes. This document contains ...
违约概率风险EDF 计算说明 首先,本文采用Bharath and Shumway(2008)提出的Naive模型估计违约概率(EDF)作为违约风险 的替代变量,我们采取...我们设置虚拟变量Violate表示企业事后违约概率。 当该差额大于零时
Ace - Responsive Admin Template 的最新版本,一个不错的后台管理模板,其实和1.3.4版本界面上基本上一样,个人还没发现不同的地方,感觉这个模板做的不错,好多人都在研究学习。
Customize Junos software to detect and block commits that violate your network standards Develop custom CLI commands to present information the way you want Program Junos software to automatically ...
详细描述关于votatile关键字使用,可以帮助你加深对volatile关键字的理解和使用。
However, the publishers of international editions generally do not authorize the sale and distribution of international editions in Canada, and such sale or distribution may violate the copyrights ...
publication may violate copyright, trademark, and other laws. Except as specified in this permission statement, this publication may not be copied, reproduced, modified, published, uploaded, posted, ...
But this time I would like to violate it for the following reasons. First of all, for me EDBED is the important milestone in the development of empirical inference theory and I would like to explain ...
Intel may terminate this Agreement at any time if you violate its terms. Upon termination, you will immediately destroy the Software or return all copies of the Software to Intel. APPLICABLE LAWS. ...
违规行为 描述 获取违反验证和断言的... let Violate = require ( 'violations' ) . Violate ; let rule = { name : ( val ) => { if ( val === undefined ) { return 'name is required' ; } } , age : ( val
violate和synchronize wait和notify 线程池 锁 源码分析concurrent包 ConcurrentHashMap CopyOnWriteArrayList BlockingQeque ThreadLocal 反射 Kotlin Kotlin的优势 协程 Android热门技术 代码插桩技术 动态代理 ...
An integrated circuit (IC) connected... Additional external protection devices can violate stringent signaling requirements, leaving design engineers with the need to balance performance and reliability.
volatile这个关键字可能很多朋友都听说过,或许也都用过。在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果,本文给大家介绍java中volatile关键字,需要的朋友参考下
Covert assessment in psychoeducational testing COVERT ASSESSMENT I N ...is a procedure which does not violate standardized testing procedures, but rather complements and enhances the assessment pr
Covert assessment in psychoeducational testing COVERT ASSESSMENT I N ...is a procedure which does not violate standardized testing procedures, but rather complements and enhances the assessment pr
We hold no responsibilty of any use that violate Instagram's terms and conditions. Installation $ go get github.com/hieven/go-instagram Documentation Instagram Instagram Models Features You can use...
Reusing those resources for other tasks is rarely donein production services since the contention for shared resourcescan cause latency spikes that violate the service-level objectiv
The proposed repair operator is used to fix the solutions that violate the box constraints. More specifically, it employs a reversed correction strategy that can effectively avoid the population ...