AtomicInteger lazySet与set [英] AtomicInteger lazySet vs. set

查看:1465
本文介绍了AtomicInteger lazySet与set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

lazySet 设定方法之间的区别是 AtomicInteger 文档没有太多关于 lazySet 的说法:

What is the difference between the lazySet and set methods of AtomicInteger? The documentation doesn't have much to say about lazySet:


到给定值。

Eventually sets to the given value.

似乎存储的值不会立即设置为所需的值,设置一些时间在未来。但是,这种方法的实际用途是什么?任何示例?

It seems that the stored value will not be immediately set to the desired value but will instead be scheduled to be set some time in the future. But, what is the practical use of this method? Any example?

推荐答案

直接从错误6275329


为Mustang,
添加了一个lazySet方法到原子类
(AtomicInteger,AtomicReference等)。这是一个niche
方法,有时在使用
非阻塞数据结构进行微调代码时很有用。语义是
,写保证不与任何
以前的写入重新排序,但是可以用后续操作重新排序
(或者等效地,可能对其他线程不可见)直到
一些其他的易失性写入或同步动作发生)。

As probably the last little JSR166 follow-up for Mustang, we added a "lazySet" method to the Atomic classes (AtomicInteger, AtomicReference, etc). This is a niche method that is sometimes useful when fine-tuning code using non-blocking data structures. The semantics are that the write is guaranteed not to be re-ordered with any previous write, but may be reordered with subsequent operations (or equivalently, might not be visible to other threads) until some other volatile write or synchronizing action occurs).

主要用例是用于对
非阻塞数据结构只是为了避免长期垃圾回收
;它适用于当它是无害
如果其他线程看到非null值一段时间,但你会
喜欢确保结构是最终GCable。在这种
的情况下,你可以通过避免
的null volatile-write的成本来获得更好的性能。还有一些
的其他用例,沿着这些行非基于参考的
原子,因此该方法支持所有的
AtomicX类。

The main use case is for nulling out fields of nodes in non-blocking data structures solely for the sake of avoiding long-term garbage retention; it applies when it is harmless if other threads see non-null values for a while, but you'd like to ensure that structures are eventually GCable. In such cases, you can get better performance by avoiding the costs of the null volatile-write. There are a few other use cases along these lines for non-reference-based atomics as well, so the method is supported across all of the AtomicX classes.

对于喜欢在公共多处理器上的
机器级障碍方面考虑这些操作的人,lazySet
提供了前面的存储 - 存储障碍b $ ba no-op或在当前平台上非常便宜),但没有
存储负载屏障(这通常是易失性写入的昂贵的部分
)。

For people who like to think of these operations in terms of machine-level barriers on common multiprocessors, lazySet provides a preceeding store-store barrier (which is either a no-op or very cheap on current platforms), but no store-load barrier (which is usually the expensive part of a volatile-write).

这篇关于AtomicInteger lazySet与set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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