1、概述
Atomic原子类位于java.util.concurrent.atomic包下,支持在单个变量上解除锁的线程安全编程。事实上,此包中的类可以将volatile值、字段和数组元素的概念扩展到那些也提供原子条件更换操作的类。
2、详情
可以将包中的类分为五类:
- 基本类型:AtomicBoolean、AtomicInteger、AtomicLong
- 引用类型:AtomicReference、AtomicStampedRerence、AtomicMarkableReference
- 数组:AtomicIntegerArray、AtomicLongArray、AtomicReferenceArray
- 对象的属性:AtomicIntegerFieldUpdater、AtomicLongFieldUpdater、AtomicReferenceFieldUpdater
- JDK1.8新增:DoubleAccumulator、LongAccumulator、DoubleAdder、LongAdder
AtomicBoolean、AtomicInteger、AtomicLong 和 AtomicReference 的实例各自提供对相应类型单个变量的原子方式访问和更新功能。例如 AtomicBoolean 提供对int类型单个变量的原子方式访问和更新功能。每个类也为该类型提供适当的实用工具方法。例如,类 AtomicLong 和 AtomicInteger 提供了原子增量方法,可以用于生成序列号。
AtomicStampedRerence 维护带有整数“标志”的对象引用,可以用原子方式对其进行更新。AtomicMarkableReference 维护带有标记位的对象引用,可以原子方式对其进行更新。
AtomicIntegerArray、AtomicLongArray 和 AtomicReferenceArray 类进一步扩展了原子操作,对这些类型的数组提供了支持。例如 AtomicIntegerArray 是可以用原子方式更新其元素的int数组。
AtomicReferenceFieldUpdater、AtomicIntegerFieldUpdater和AtomicLongFieldUpdater是基于反射的实用工具,可以提供对关联字段类型的访问。例如AtomicIntegerFieldUpdater可以对指定类的指定volatile int字段进行原子更新。
DoubleAccumulator、LongAccumulator、DoubleAdder、LongAdder是JDK1.8新增的部分,是对AtomicLong等类的改进。比如LongAccumulator与LongAdder在高并发环境下比AtomicLong更高效。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。