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

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

问题描述

AtomicIntegerlazySetset 方法有什么区别?文档lazySet 没什么好说的:

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

最终设置为给定的值.

似乎存储的值不会立即设置为所需的值,而是会安排在将来的某个时间设置.但是,这种方法的实际用途是什么?有什么例子吗?

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?

推荐答案

直接引用自 JDK-6275329:向原子类添加lazySet方法":

可能是 Mustang 的最后一个 JSR166 后续小程序,我们在原子类中添加了一个lazySet"方法(AtomicInteger、AtomicReference 等).这是一个利基有时在使用微调代码时很有用的方法非阻塞数据结构.语义是保证写入不会与任何重新排序之前的写入,但可能会在后续操作中重新排序(或等效地,可能对其他线程不可见)直到发生其他一些易失性写入或同步操作).

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).

主要用例是将节点的字段清零非阻塞数据结构只是为了避免长期垃圾保留;它在无害时适用如果其他线程在一段时间内看到非空值,但您会喜欢确保结构最终是可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提供了一个先前的商店-商店屏障(要么是在当前平台上无操作或非常便宜),但没有存储负载屏障(这通常是昂贵的部分易失性写入).

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天全站免登陆