java并发:原子操作Atomic
One of the best additions in java 5 was Atomic operations supported in classes such as AtomicInteger
, AtomicLong
etc.
These classes internally rely on an algorithm named CAS (compare and swap).
原子操作Atomic
源码解析
AtomicInteger是基于sun.misc.Unsafe来实现的,其定义如下:
该类中有如下一系列方法:
其中某些方法的实现如下:
AtomicReference
此处再介绍一下AtomicReference,其定义如下:
该类使用了泛型,在定义该类型的变量时需要指定泛型实参,如下:
public
final
static
AtomicReference
new
AtomicReference
"abc"
);
其中String即泛型实参