- The basic categories:
- AtomicInteger
- AtomicLong
- AtomicBoolean
- Array type:
- AtomicIntegerArray
- AtomicLongArray
- AtomicReferenceArray
Introduction
Because in multithreaded conditions, if you modify the shared variables likely to cause data inconsistencies, so for shared variables we need to ensure the security thread has the following ways:
- Use
lock
orsynchronized
synchronize shared variables - CAS is used to modify variables methods to ensure atomicity operation
The latter class is based on the CAS modify atomic.
The principle
- Converting the boolean true to
int
type indicates: 1 represents 0 represents false true - When initialized in the class to get the value of the memory address
- Calling
Unsafe.compareAndSwant
method changes the underlying principle of CAS values (CPU instruction in cmpxchg)
Feature
- Based on CAS achieve thread safety
- Implements
Cloneable
the interface, be cloned - It implements
Serializable
the interface, support serialization transmission
Source resolve
Member variables
private static final long serialVersionUID = 4654671469794556979L;
// setup to use Unsafe.compareAndSwapInt for updates
//使用unsafe类进行cas
private static final Unsafe unsafe = Unsafe.getUnsafe();
//获取该值得偏移量(内存中的地址)
private static final long valueOffset;
/**
* 内部使用int来做boolean的设置
* 默认为0
*/
private volatile int value;
serialVersionUID
: Sequence ID-unsafe
: Atomic core class is class, for performing low-level, the operation of the memory, all internalnative
methodsvalueOffset
: Memory offset address field valuevalue
: True value, 1 represents true 0 represents false, usevolatile
to ensure visibility of memory
Class initialization process
static {
try {
//返回对象成员属性在内存地址相对于此对象的内存地址的偏移量
valueOffset = unsafe.objectFieldOffset
(AtomicBoolean.class.getDeclaredField("value"));
} catch (Exception ex) { throw new Error(ex); }
}
The main memory is to get value worth offset by unsafe methods
Member method
get()
Gets the boolean variable
/**
* 返回当前值
*/
public final boolean get() {
return value != 0;
}
boolean compareAndSet(boolean expect, boolean update)
Assignment, there may be value after comparing the situation before the assignment failure
/*
* 只有当期待的值为expect的时候才会更新相关值
* 1. 期待的值等于现在值,则成功赋值,返回true
* 2. 期待的值不等于现在的值,则赋值失败,则返回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);
}
- Converting an int boolean
- Call to
compareAndSwapInt
be assigned CAS - Returns true for success, false indicates failure
boolean getAndSet(boolean newValue)
比较前值后进行赋值,用的相对较多
public final boolean getAndSet(boolean newValue) {
boolean prev;
do {
prev = get();
} while (!compareAndSet(prev, newValue));
return prev;
}
- 先获取之前值
- 在调用循环
compareAndSet
进行CAS赋值
void set(boolean newValue)
无条件设置值,用的相对较少
public final void set(boolean newValue) {
value = newValue ? 1 : 0;
}
void lazySet(boolean newValue)
也是赋值操作,该操作会让Java插入StoreStore内存屏障,避免发生写操作重排序
public final void lazySet(boolean newValue) {
int v = newValue ? 1 : 0;
unsafe.putOrderedInt(this, valueOffset, v);
}
总结
- 该类是原子性
boolean
类,是线程安全的 - 该原子类的核心操作都是基于Unsafe类
- CAS普遍会产ABA问题