gpt4 book ai didi

java - AtomicInteger lazySet 与 set

转载 作者:IT老高 更新时间:2023-10-28 11:22:47 30 4
gpt4 key购买 nike

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

Eventually sets to the given value.

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

最佳答案

直接引自 "JDK-6275329: Add lazySet methods to atomic classes" :

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

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.

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

关于java - AtomicInteger lazySet 与 set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1468007/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com