void lazySet(int newValue):使用此方法后最终会被设置成newValue。是线程不安全的。官方解释如下:
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).
这里解释道:此方法不可与之前的写操作进行重排序,可以与之后的写操作进行重排序,知道出现volatile写或synchronizing操作。好处是比普通的set方法性能要好,前提是可以忍受其他线程在一段时间内读到的是旧数据。
int getAndSet(int newValue):以原子方式更新,并且返回旧值。
boolean compareAndSet(int expect, int update):如果输入的值等于expect的值,则以原子方式更新。
int getAndIncrement():以原子方式自增,返回的是自增前的值。
int getAndDecrement():与getAndIncrement相反,返回的是自减前的值。
int getAndAdd(int delta):以原子方式,将当前值与输入值相加,返回的是计算前的值。
int incrementAndGet():以原子方式自增,返回自增后的值。
int decrementAndGet():以原子方式自减,返回自减后的值。
int addAndGet(int delta):以原子方式,将当前值与输入值相加,返回的是计算后的值。
int getAndUpdate(IntUnaryOperator updateFunction):Java1.8新增方法,以原子方式,按照指定方法更新当前数值,返回更新前的值,需要注意的是,提供的方法应该无副作用(side-effect-free),即两次执行结果相同,原因是如果由于线程争用导致更新失败会尝试再次执行该方法。
int updateAndGet(IntUnaryOperator updateFunction):同样是Java1.8新增方法,与getAndUpdate唯一不同的是返回值是更新后的值。
int getAndAccumulate(int x, IntBinaryOperator accumulatorFunction):与上述两个方法类似,操作数由参数x提供。返回更新前的值。
int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction):与getAndAccumulate方法作用相同,返回更新后的值。
/**
* Atomically increments by one the current value.
*
* @return the previous value
*/
public final int getAndIncrement() {
return unsafe.getAndAddInt(this, valueOffset, 1);
}
继续看Unsafe方法里的getAndAddInt方法
代码语言:javascript
复制
public final int getAndAddInt(Object var1, long var2, int var4) {
int var5;
do {
var5 = this.getIntVolatile(var1, var2);
} while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4));
return var5;
}
/**
* Atomically sets the value to the given updated value
* if the current value {@code ==} the expected value.
* @param expect the expected value
* @param update the new value
* @return {@code true} if successful. False return indicates that
* the actual value was not equal to the expected value.
*/
public final boolean compareAndSet(V expect, V update) {
return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
}