现实生活中对AtomicLongFieldUpdate类的使用和说明 [英] Real life use and explanation of the AtomicLongFieldUpdate class
问题描述
有人知道对类在现实生活中的任何使用AtomicLongFieldUpdate ? 我已经阅读了说明,但是还没有完全理解它的含义. 我为什么要知道那件事?好奇心和OCPJP准备.
Is anybody aware of any real life use of the class AtomicLongFieldUpdate? I have read the description but I have not quite grasped the meaning of it. Why do I want to know that? Curiosity and for OCPJP preparation.
先谢谢了.
推荐答案
您可以考虑以下成本阶梯:
You can think of a cost ladder for the following:
- 普通
long
:便宜,但对多线程访问不安全 -
volatile long
:价格昂贵,可以安全地进行多线程访问,无法进行原子操作 -
AtomicLong
:最昂贵,最安全的多线程访问,可能的原子操作
- ordinary
long
: cheap, but unsafe for multi-threaded access volatile long
: more expensive, safe for multi-threaded access, atomic operations not possibleAtomicLong
: most expensive, safe for multi-threaded access, atomic operations possible
(当我说不安全"或不可能"时,我的意思是没有像同步这样的外部机制".)
(When I say 'unsafe' or 'not possible' I mean 'without an external mechanism like synchronization' of course.)
在需要多线程访问但大多数操作是简单的读写的情况下,只需执行几个原子操作,就可以创建一个AtomicLongFieldUpdate
静态实例,并在需要原子更新时使用它.然后,内存/运行时开销类似于简单的volatile
变量,除了原子操作的数量级(或比普通AtomicLong
操作的开销更高).
In the case where multi-threaded access is needed, but most operations are simple reads or writes, with only a few atomic operations needed, you can create one static instance of AtomicLongFieldUpdate
and use this when atomic updates are needed. The memory/runtime overhead is then similar to a simple volatile
variable, except for the atomic operations which are of the order of (or slightly more expensive than) the ordinary AtomicLong
operations.
这是不错的小教程.
这篇关于现实生活中对AtomicLongFieldUpdate类的使用和说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!