【Java并发】原子类源码分析之AtomicBoolean
JDK1.8
public class AtomicBoolean implements java.io.Serializable {
private static final long serialVersionUID = 4654671469794556979L;
// setup to use Unsafe.compareAndSwapInt for updates
// 使用Unsafe.compareAndSwapInt进行更新
private static final Unsafe unsafe = Unsafe.getUnsafe();
// valueOffset是对象在内存中的偏移量
private static final long valueOffset;
//初始阶段,通过unsafe来获取AtomicBoolean类的value字段在内存中的偏移量valueOffset
static {
try {
valueOffset = unsafe.objectFieldOffset
(AtomicBoolean.class.getDeclaredField("value"));
} catch (Exception ex) { throw new Error(ex); }
}
//value被volatile修饰,保证其内存可见性,后面也会以value来判断AtomicBoolean值
private volatile int value;
/**
* Creates a new {@code AtomicBoolean} with the given initial value.
*
* @param initialValue the initial value
*/
//构造方法:支持设置初始值,并且以int来存储 true -> 1 false -> 0
public AtomicBoolean(boolean initialValue) {
value = initialValue ? 1 : 0;
}
/**
* Creates a new {@code AtomicBoolean} with initial value {@code false}.
*/
//构造方法:没有赋值,没赋值的话,int默认为0, 0 -> false
public AtomicBoolean() {
}
/**
* Returns the current value.
*
* @return the current value
*/
//获取当前value值 final修饰
public final boolean get() {
return value != 0;
}
/**
* 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.
*/
//如果当前值 == 期望值expect,便会原子地更新成update值,返回true
//如果当前值 != 期望值expect,更新失败,返回false
public final boolean compareAndSet(boolean expect, boolean update) {
int e = expect ? 1 : 0;
int u = update ? 1 : 0;
return unsafe.compareAndSwapInt(this, valueOffset, e, u);
}
/**
* Atomically sets the value to the given updated value
* if the current value {@code ==} the expected value.
*
* <p><a href="package-summary.html#weakCompareAndSet">May fail
* spuriously and does not provide ordering guarantees</a>, so is
* only rarely an appropriate alternative to {@code compareAndSet}.
*
* @param expect the expected value
* @param update the new value
* @return {@code true} if successful
*/
//是的,这里和compareAndSet是一模一样的实现
//有兴趣的可以百度下
public boolean weakCompareAndSet(boolean expect, boolean update) {
int e = expect ? 1 : 0;
int u = update ? 1 : 0;
return unsafe.compareAndSwapInt(this, valueOffset, e, u);
}
/**
* Unconditionally sets to the given value.
*
* @param newValue the new value
*/
//不带任何条件地进行赋值,虽然value被volatile修饰,但是set这里修改不是原子方法
public final void set(boolean newValue) {
value = newValue ? 1 : 0;
}
/**
* Eventually sets to the given value.
*
* @param newValue the new value
* @since 1.6
*/
//注释的含义是:最终设置成功
//和set方法对比来看,lazySet底层调用的是unsafe.putOrderedInt
//从hotspot源码看putOrderedXXX源码分析底层操作
//可以参考这个同学的分析,https://www.jianshu.com/p/4ed887664b13
public final void lazySet(boolean newValue) {
int v = newValue ? 1 : 0;
unsafe.putOrderedInt(this, valueOffset, v);
}
/**
* Atomically sets to the given value and returns the previous value.
*
* @param newValue the new value
* @return the previous value
*/
//原子地更新value值为newValue,并返回更新之前的值
//先获取更新前的值,然后while一直尝试更新直到成功为止
public final boolean getAndSet(boolean newValue) {
boolean prev;
do {
prev = get();
} while (!compareAndSet(prev, newValue));
return prev;
}
/**
* Returns the String representation of the current value.
* @return the String representation of the current value
*/
public String toString() {
return Boolean.toString(get());
}
}