在java4和java5中使用volatile关键字 [英] using volatile keyword in java4 and java5

查看:130
本文介绍了在java4和java5中使用volatile关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java4和java5之后使用 volatile关键字有什么不同?

what is the difference in using volatile keyword in java4 and java5 onwards?

并且与此相关,


非原子变量(long / double)的读/写操作是
声明为volatile的原子变量。

Read/write operations on non-atomic variables(long/double) are atomic when they are declared as volatile.

对于java4 它是否也适用于从java5开始有效???

Is this also true for java4 or it is valid from java5 onwards???

推荐答案

人们提供了积分和参考资料来回答我的问题,回答第一部分。

People have provided good points and references responding to my question answering first part.

具体到问题的第二部分,我在一些论坛上读到:

Going specific to the second part of question, this i read at some forum:


声明为long的volatile是原子的(在Java 5之前也是如此) )在
的意义上,它保证(对于所有JVM实现)读或写直接转到主存储器而不是两个32位寄存器。

A volatile declared long is atomic (pre-Java 5 also) in the sense that it guarantees (for all JVM implementations) a read or write go directly to main memory instead of two 32-bit registers.


Pre-Java 5,volatile是支持sed 为长
和double提供此类保证。但是在实践中事​​情并没有这样做,并且
实现经常违反此保证。我记得
问题似乎已经解决了JDK 1.4的问题,但是由于它们仍然在整个内存模型上工作,但他们并没有真正做出任何
的明确公告。 JDK 5,当新规则宣布为
时,内存保证实际意味着什么。

Pre-Java 5, volatile was supposed to provide such guarantees for long and double. However things did not work out this way in practice, and implementations frequently violated this guarantee. As I recall the issue seemed to get fixed around JDK 1.4, but as they were still working on the whole memory model thing, they didn't really make any clear announcements about it until JDK 5, when the new rules were announced, and memory guarantees actually meant something.

这是来自 Java语言规范,第二版


17.4双和长的非原子性处理

17.4 Nonatomic Treatment of double and long

对volatile变量的加载,存储,读取和写入操作都是原子的,
,即使变量的类型是double或long。

The load, store, read, and write actions on volatile variables are atomic, even if the type of the variable is double or long.

这篇关于在java4和java5中使用volatile关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆