JavaGuide/docs/java/concurrent/atomic-classes.md

451 lines
17 KiB
Markdown
Raw Permalink Normal View History

---
title: Atomic 原子类总结
category: Java
tag:
- Java并发
---
## Atomic 原子类介绍
Atomic 翻译成中文是原子的意思。在化学上,我们知道原子是构成一般物质的最小单位,在化学反应中是不可分割的。在我们这里 Atomic 是指一个操作是不可中断的。即使是在多个线程一起执行的时候,一个操作一旦开始,就不会被其他线程干扰。
所以,所谓原子类说简单点就是具有原子/原子操作特征的类。
并发包 `java.util.concurrent` 的原子类都存放在`java.util.concurrent.atomic`下,如下图所示。
2023-08-14 09:05:59 +08:00
![JUC原子类概览](https://oss.javaguide.cn/github/javaguide/java/JUC%E5%8E%9F%E5%AD%90%E7%B1%BB%E6%A6%82%E8%A7%88.png)
根据操作的数据类型,可以将 JUC 包中的原子类分为 4 类
**基本类型**
使用原子的方式更新基本类型
2023-01-31 16:27:36 +08:00
- `AtomicInteger`:整型原子类
- `AtomicLong`:长整型原子类
2023-05-05 12:39:01 +08:00
- `AtomicBoolean`:布尔型原子类
**数组类型**
使用原子的方式更新数组里的某个元素
2023-01-31 16:27:36 +08:00
- `AtomicIntegerArray`:整型数组原子类
- `AtomicLongArray`:长整型数组原子类
2023-05-05 12:39:01 +08:00
- `AtomicReferenceArray`:引用类型数组原子类
**引用类型**
2023-01-31 16:27:36 +08:00
- `AtomicReference`:引用类型原子类
- `AtomicMarkableReference`:原子更新带有标记的引用类型。该类将 boolean 标记与引用关联起来,~~也可以解决使用 CAS 进行原子更新时可能出现的 ABA 问题~~。
2023-05-05 12:39:01 +08:00
- `AtomicStampedReference`:原子更新带有版本号的引用类型。该类将整数值与引用关联起来,可用于解决原子的更新数据和数据的版本号,可以解决使用 CAS 进行原子更新时可能出现的 ABA 问题。
2020-02-20 21:09:49 +08:00
2023-01-31 16:27:36 +08:00
**🐛 修正(参见:[issue#626](https://github.com/Snailclimb/JavaGuide/issues/626)** : `AtomicMarkableReference` 不能解决 ABA 问题。
2023-01-31 16:27:36 +08:00
**对象的属性修改类型**
2023-01-31 16:27:36 +08:00
- `AtomicIntegerFieldUpdater`:原子更新整型字段的更新器
- `AtomicLongFieldUpdater`:原子更新长整型字段的更新器
- `AtomicReferenceFieldUpdater`:原子更新引用类型里的字段
## 基本类型原子类
使用原子的方式更新基本类型
2023-01-31 16:27:36 +08:00
- `AtomicInteger`:整型原子类
- `AtomicLong`:长整型原子类
2023-05-05 12:39:01 +08:00
- `AtomicBoolean`:布尔型原子类
2023-01-31 16:27:36 +08:00
上面三个类提供的方法几乎相同,所以我们这里以 `AtomicInteger` 为例子来介绍。
**AtomicInteger 类常用方法**
2019-06-17 18:03:13 +08:00
```java
public final int get() //获取当前的值
public final int getAndSet(int newValue)//获取当前的值,并设置新的值
public final int getAndIncrement()//获取当前的值,并自增
public final int getAndDecrement() //获取当前的值,并自减
public final int getAndAdd(int delta) //获取当前的值,并加上预期的值
boolean compareAndSet(int expect, int update) //如果输入的数值等于预期值则以原子方式将该值设置为输入值update
public final void lazySet(int newValue)//最终设置为newValue,使用 lazySet 设置之后可能导致其他线程在之后的一小段时间内还是可以读到旧的值。
```
2023-01-31 16:27:36 +08:00
**`AtomicInteger` 类使用示例** :
```java
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicIntegerTest {
2023-01-31 16:27:36 +08:00
public static void main(String[] args) {
int temvalue = 0;
AtomicInteger i = new AtomicInteger(0);
temvalue = i.getAndSet(3);
System.out.println("temvalue:" + temvalue + "; i:" + i); //temvalue:0; i:3
temvalue = i.getAndIncrement();
System.out.println("temvalue:" + temvalue + "; i:" + i); //temvalue:3; i:4
temvalue = i.getAndAdd(5);
System.out.println("temvalue:" + temvalue + "; i:" + i); //temvalue:4; i:9
}
}
```
### 基本数据类型原子类的优势
通过一个简单例子带大家看一下基本数据类型原子类的优势
2023-01-31 16:27:36 +08:00
**1、多线程环境不使用原子类保证线程安全基本数据类型**
```java
class Test {
private volatile int count = 0;
//若要线程安全执行执行count++,需要加锁
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}
```
2023-01-31 16:27:36 +08:00
**2、多线程环境使用原子类保证线程安全基本数据类型**
```java
class Test2 {
private AtomicInteger count = new AtomicInteger();
public void increment() {
count.incrementAndGet();
}
//使用AtomicInteger之后不需要加锁也可以实现线程安全。
public int getCount() {
return count.get();
}
}
```
### AtomicInteger 线程安全原理简单分析
2023-01-31 16:27:36 +08:00
`AtomicInteger` 类的部分源码:
```java
// setup to use Unsafe.compareAndSwapInt for updates更新操作时提供“比较并替换”的作用
private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final long valueOffset;
static {
try {
valueOffset = unsafe.objectFieldOffset
(AtomicInteger.class.getDeclaredField("value"));
} catch (Exception ex) { throw new Error(ex); }
}
private volatile int value;
```
2023-01-31 16:27:36 +08:00
`AtomicInteger` 类主要利用 CAS (compare and swap) + volatile 和 native 方法来保证原子操作,从而避免 synchronized 的高开销,执行效率大为提升。
2023-01-31 16:27:36 +08:00
CAS 的原理是拿期望的值和原本的一个值作比较如果相同则更新成新的值。UnSafe 类的 `objectFieldOffset()` 方法是一个本地方法,这个方法是用来拿到“原来的值”的内存地址。另外 value 是一个 volatile 变量,在内存中可见,因此 JVM 可以保证任何时刻任何线程总能拿到该变量的最新值。
## 数组类型原子类
使用原子的方式更新数组里的某个元素
2023-01-31 16:27:36 +08:00
- `AtomicIntegerArray`:整形数组原子类
- `AtomicLongArray`:长整形数组原子类
2023-05-05 12:39:01 +08:00
- `AtomicReferenceArray`:引用类型数组原子类
2023-01-31 16:27:36 +08:00
上面三个类提供的方法几乎相同,所以我们这里以 `AtomicIntegerArray` 为例子来介绍。
2023-05-05 12:39:01 +08:00
**`AtomicIntegerArray` 类常用方法**
```java
public final int get(int i) //获取 index=i 位置元素的值
public final int getAndSet(int i, int newValue)//返回 index=i 位置的当前的值并将其设置为新值newValue
public final int getAndIncrement(int i)//获取 index=i 位置元素的值,并让该位置的元素自增
public final int getAndDecrement(int i) //获取 index=i 位置元素的值,并让该位置的元素自减
2020-05-10 10:01:27 +08:00
public final int getAndAdd(int i, int delta) //获取 index=i 位置元素的值,并加上预期的值
boolean compareAndSet(int i, int expect, int update) //如果输入的数值等于预期值,则以原子方式将 index=i 位置的元素值设置为输入值update
public final void lazySet(int i, int newValue)//最终 将index=i 位置的元素设置为newValue,使用 lazySet 设置之后可能导致其他线程在之后的一小段时间内还是可以读到旧的值。
```
2023-01-31 16:27:36 +08:00
**`AtomicIntegerArray` 类使用示例** :
```java
import java.util.concurrent.atomic.AtomicIntegerArray;
public class AtomicIntegerArrayTest {
2023-01-31 16:27:36 +08:00
public static void main(String[] args) {
int temvalue = 0;
int[] nums = { 1, 2, 3, 4, 5, 6 };
AtomicIntegerArray i = new AtomicIntegerArray(nums);
for (int j = 0; j < nums.length; j++) {
System.out.println(i.get(j));
}
temvalue = i.getAndSet(0, 2);
System.out.println("temvalue:" + temvalue + "; i:" + i);
temvalue = i.getAndIncrement(0);
System.out.println("temvalue:" + temvalue + "; i:" + i);
temvalue = i.getAndAdd(0, 5);
System.out.println("temvalue:" + temvalue + "; i:" + i);
}
}
```
## 引用类型原子类
基本类型原子类只能更新一个变量,如果需要原子更新多个变量,需要使用 引用类型原子类。
2023-01-31 16:27:36 +08:00
- `AtomicReference`:引用类型原子类
- `AtomicStampedReference`:原子更新带有版本号的引用类型。该类将整数值与引用关联起来,可用于解决原子的更新数据和数据的版本号,可以解决使用 CAS 进行原子更新时可能出现的 ABA 问题。
2023-05-05 12:39:01 +08:00
- `AtomicMarkableReference`:原子更新带有标记的引用类型。该类将 boolean 标记与引用关联起来,~~也可以解决使用 CAS 进行原子更新时可能出现的 ABA 问题。~~
2023-01-31 16:27:36 +08:00
上面三个类提供的方法几乎相同,所以我们这里以 `AtomicReference` 为例子来介绍。
2023-01-31 16:27:36 +08:00
**`AtomicReference` 类使用示例** :
```java
import java.util.concurrent.atomic.AtomicReference;
public class AtomicReferenceTest {
2023-01-31 16:27:36 +08:00
public static void main(String[] args) {
AtomicReference < Person > ar = new AtomicReference < Person > ();
Person person = new Person("SnailClimb", 22);
ar.set(person);
Person updatePerson = new Person("Daisy", 20);
ar.compareAndSet(person, updatePerson);
System.out.println(ar.get().getName());
System.out.println(ar.get().getAge());
}
}
class Person {
2023-01-31 16:27:36 +08:00
private String name;
private int age;
2023-01-31 16:27:36 +08:00
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
2023-01-31 16:27:36 +08:00
public String getName() {
return name;
}
2023-01-31 16:27:36 +08:00
public void setName(String name) {
this.name = name;
}
2023-01-31 16:27:36 +08:00
public int getAge() {
return age;
}
2023-01-31 16:27:36 +08:00
public void setAge(int age) {
this.age = age;
}
}
```
2023-01-31 16:27:36 +08:00
上述代码首先创建了一个 `Person` 对象,然后把 `Person` 对象设置进 `AtomicReference` 对象中,然后调用 `compareAndSet` 方法,该方法就是通过 CAS 操作设置 ar。如果 ar 的值为 `person` 的话,则将其设置为 `updatePerson`。实现原理与 `AtomicInteger` 类中的 `compareAndSet` 方法相同。运行上面的代码后的输出结果如下:
2023-10-08 16:33:50 +08:00
```plain
Daisy
20
```
2023-01-31 16:27:36 +08:00
**`AtomicStampedReference` 类使用示例** :
```java
import java.util.concurrent.atomic.AtomicStampedReference;
public class AtomicStampedReferenceDemo {
public static void main(String[] args) {
// 实例化、取当前值和 stamp 值
final Integer initialRef = 0, initialStamp = 0;
final AtomicStampedReference<Integer> asr = new AtomicStampedReference<>(initialRef, initialStamp);
System.out.println("currentValue=" + asr.getReference() + ", currentStamp=" + asr.getStamp());
// compare and set
final Integer newReference = 666, newStamp = 999;
final boolean casResult = asr.compareAndSet(initialRef, newReference, initialStamp, newStamp);
System.out.println("currentValue=" + asr.getReference()
+ ", currentStamp=" + asr.getStamp()
+ ", casResult=" + casResult);
// 获取当前的值和当前的 stamp 值
int[] arr = new int[1];
final Integer currentValue = asr.get(arr);
final int currentStamp = arr[0];
System.out.println("currentValue=" + currentValue + ", currentStamp=" + currentStamp);
// 单独设置 stamp 值
final boolean attemptStampResult = asr.attemptStamp(newReference, 88);
System.out.println("currentValue=" + asr.getReference()
+ ", currentStamp=" + asr.getStamp()
+ ", attemptStampResult=" + attemptStampResult);
// 重新设置当前值和 stamp 值
asr.set(initialRef, initialStamp);
System.out.println("currentValue=" + asr.getReference() + ", currentStamp=" + asr.getStamp());
// [不推荐使用,除非搞清楚注释的意思了] weak compare and set
// 困惑weakCompareAndSet 这个方法最终还是调用 compareAndSet 方法。[版本: jdk-8u191]
// 但是注释上写着 "May fail spuriously and does not provide ordering guarantees,
// so is only rarely an appropriate alternative to compareAndSet."
// todo 感觉有可能是 jvm 通过方法名在 native 方法里面做了转发
final boolean wCasResult = asr.weakCompareAndSet(initialRef, newReference, initialStamp, newStamp);
System.out.println("currentValue=" + asr.getReference()
+ ", currentStamp=" + asr.getStamp()
+ ", wCasResult=" + wCasResult);
}
}
```
输出结果如下:
2023-10-08 16:33:50 +08:00
```plain
currentValue=0, currentStamp=0
currentValue=666, currentStamp=999, casResult=true
currentValue=666, currentStamp=999
currentValue=666, currentStamp=88, attemptStampResult=true
currentValue=0, currentStamp=0
currentValue=666, currentStamp=999, wCasResult=true
```
2023-01-31 16:27:36 +08:00
**`AtomicMarkableReference` 类使用示例** :
```java
import java.util.concurrent.atomic.AtomicMarkableReference;
public class AtomicMarkableReferenceDemo {
public static void main(String[] args) {
// 实例化、取当前值和 mark 值
final Boolean initialRef = null, initialMark = false;
final AtomicMarkableReference<Boolean> amr = new AtomicMarkableReference<>(initialRef, initialMark);
System.out.println("currentValue=" + amr.getReference() + ", currentMark=" + amr.isMarked());
// compare and set
final Boolean newReference1 = true, newMark1 = true;
final boolean casResult = amr.compareAndSet(initialRef, newReference1, initialMark, newMark1);
System.out.println("currentValue=" + amr.getReference()
+ ", currentMark=" + amr.isMarked()
+ ", casResult=" + casResult);
// 获取当前的值和当前的 mark 值
boolean[] arr = new boolean[1];
final Boolean currentValue = amr.get(arr);
final boolean currentMark = arr[0];
System.out.println("currentValue=" + currentValue + ", currentMark=" + currentMark);
// 单独设置 mark 值
final boolean attemptMarkResult = amr.attemptMark(newReference1, false);
System.out.println("currentValue=" + amr.getReference()
+ ", currentMark=" + amr.isMarked()
+ ", attemptMarkResult=" + attemptMarkResult);
// 重新设置当前值和 mark 值
amr.set(initialRef, initialMark);
System.out.println("currentValue=" + amr.getReference() + ", currentMark=" + amr.isMarked());
// [不推荐使用,除非搞清楚注释的意思了] weak compare and set
// 困惑weakCompareAndSet 这个方法最终还是调用 compareAndSet 方法。[版本: jdk-8u191]
// 但是注释上写着 "May fail spuriously and does not provide ordering guarantees,
// so is only rarely an appropriate alternative to compareAndSet."
// todo 感觉有可能是 jvm 通过方法名在 native 方法里面做了转发
final boolean wCasResult = amr.weakCompareAndSet(initialRef, newReference1, initialMark, newMark1);
System.out.println("currentValue=" + amr.getReference()
+ ", currentMark=" + amr.isMarked()
+ ", wCasResult=" + wCasResult);
}
}
```
输出结果如下:
2023-10-08 16:33:50 +08:00
```plain
currentValue=null, currentMark=false
currentValue=true, currentMark=true, casResult=true
currentValue=true, currentMark=true
currentValue=true, currentMark=false, attemptMarkResult=true
currentValue=null, currentMark=false
currentValue=true, currentMark=true, wCasResult=true
```
## 对象的属性修改类型原子类
如果需要原子更新某个类里的某个字段时,需要用到对象的属性修改类型原子类。
2023-01-31 16:27:36 +08:00
- `AtomicIntegerFieldUpdater`:原子更新整形字段的更新器
- `AtomicLongFieldUpdater`:原子更新长整形字段的更新器
2023-05-05 12:39:01 +08:00
- `AtomicReferenceFieldUpdater`:原子更新引用类型里的字段的更新器
要想原子地更新对象的属性需要两步。第一步,因为对象的属性修改类型原子类都是抽象类,所以每次使用都必须使用静态方法 newUpdater()创建一个更新器,并且需要设置想要更新的类和属性。第二步,更新的对象属性必须使用 public volatile 修饰符。
上面三个类提供的方法几乎相同,所以我们这里以 `AtomicIntegerFieldUpdater`为例子来介绍。
2023-01-31 16:27:36 +08:00
**`AtomicIntegerFieldUpdater` 类使用示例** :
```java
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
public class AtomicIntegerFieldUpdaterTest {
2023-12-30 17:14:13 +08:00
public static void main(String[] args) {
AtomicIntegerFieldUpdater<User> a = AtomicIntegerFieldUpdater.newUpdater(User.class, "age");
2023-12-30 17:14:13 +08:00
User user = new User("Java", 22);
System.out.println(a.getAndIncrement(user));// 22
System.out.println(a.get(user));// 23
}
}
class User {
2023-12-30 17:14:13 +08:00
private String name;
public volatile int age;
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
输出结果:
2023-10-08 16:33:50 +08:00
```plain
22
23
```
2023-01-31 16:27:36 +08:00
## 参考
2020-03-25 16:32:50 +08:00
- 《Java 并发编程的艺术》
2023-10-27 06:44:02 +08:00
<!-- @include: @article-footer.snippet.md -->