@@ -36,14 +36,87 @@ Atomic 翻译成中文是原子的意思。在化学上,我们知道原子是
3636** 引用类型**
3737
3838- AtomicReference:引用类型原子类
39- - AtomicStampedRerence :原子更新引用类型里的字段原子类
39+ - AtomicStampedReference :原子更新引用类型里的字段原子类
4040- AtomicMarkableReference :原子更新带有标记位的引用类型
4141
4242** 对象的属性修改类型**
4343
4444- AtomicIntegerFieldUpdater:原子更新整型字段的更新器
4545- AtomicLongFieldUpdater:原子更新长整型字段的更新器
4646- AtomicStampedReference :原子更新带有版本号的引用类型。该类将整数值与引用关联起来,可用于解决原子的更新数据和数据的版本号,可以解决使用 CAS 进行原子更新时可能出现的 ABA 问题。
47+ - AtomicMarkableReference:原子更新带有标记的引用类型。该类将 boolean 标记与引用关联起来,也可以解决使用 CAS 进行原子更新时可能出现的 ABA 问题。
48+
49+ ** CAS ABA 问题**
50+ - 描述: 第一个线程取到了变量 x 的值 A,然后巴拉巴拉干别的事,总之就是只拿到了变量 x 的值 A。这段时间内第二个线程也取到了变量 x 的值 A,然后把变量 x 的值改为 B,然后巴拉巴拉干别的事,最后又把变量 x 的值变为 A (相当于还原了)。在这之后第一个线程终于进行了变量 x 的操作,但是此时变量 x 的值还是 A,所以 compareAndSet 操作是成功。
51+ - 例子描述(可能不太合适,但好理解): 年初,现金为零,然后通过正常劳动赚了三百万,之后正常消费了(比如买房子)三百万。年末,虽然现金零收入(可能变成其他形式了),但是赚了钱是事实,还是得交税的!
52+ - 代码例子(以``` AtomicInteger ``` 为例)
53+ ``` java
54+ import java.util.concurrent.atomic.AtomicInteger ;
55+
56+ public class AtomicIntegerDefectDemo {
57+ public static void main (String [] args ) {
58+ defectOfABA();
59+ }
60+
61+ static void defectOfABA () {
62+ final AtomicInteger atomicInteger = new AtomicInteger (1 );
63+
64+ Thread coreThread = new Thread (
65+ () - > {
66+ final int currentValue = atomicInteger. get();
67+ System . out. println(Thread . currentThread(). getName() + " ------ currentValue=" + currentValue);
68+
69+ // 这段目的:模拟处理其他业务花费的时间
70+ try {
71+ Thread . sleep(300 );
72+ } catch (InterruptedException e) {
73+ e. printStackTrace();
74+ }
75+
76+ boolean casResult = atomicInteger. compareAndSet(1 , 2 );
77+ System . out. println(Thread . currentThread(). getName()
78+ + " ------ currentValue=" + currentValue
79+ + " , finalValue=" + atomicInteger. get()
80+ + " , compareAndSet Result=" + casResult);
81+ }
82+ );
83+ coreThread. start();
84+
85+ // 这段目的:为了让 coreThread 线程先跑起来
86+ try {
87+ Thread . sleep(100 );
88+ } catch (InterruptedException e) {
89+ e. printStackTrace();
90+ }
91+
92+ Thread amateurThread = new Thread (
93+ () - > {
94+ int currentValue = atomicInteger. get();
95+ boolean casResult = atomicInteger. compareAndSet(1 , 2 );
96+ System . out. println(Thread . currentThread(). getName()
97+ + " ------ currentValue=" + currentValue
98+ + " , finalValue=" + atomicInteger. get()
99+ + " , compareAndSet Result=" + casResult);
100+
101+ currentValue = atomicInteger. get();
102+ casResult = atomicInteger. compareAndSet(2 , 1 );
103+ System . out. println(Thread . currentThread(). getName()
104+ + " ------ currentValue=" + currentValue
105+ + " , finalValue=" + atomicInteger. get()
106+ + " , compareAndSet Result=" + casResult);
107+ }
108+ );
109+ amateurThread. start();
110+ }
111+ }
112+ ```
113+ 输出内容如下:
114+ ```
115+ Thread-0 ------ currentValue=1
116+ Thread-1 ------ currentValue=1, finalValue=2, compareAndSet Result=true
117+ Thread-1 ------ currentValue=2, finalValue=1, compareAndSet Result=true
118+ Thread-0 ------ currentValue=1, finalValue=2, compareAndSet Result=true
119+ ```
47120
48121下面我们来详细介绍一下这些原子类。
49122
@@ -210,7 +283,7 @@ public class AtomicIntegerArrayTest {
210283基本类型原子类只能更新一个变量,如果需要原子更新多个变量,需要使用 引用类型原子类。
211284
212285- AtomicReference:引用类型原子类
213- - AtomicStampedRerence :原子更新引用类型里的字段原子类
286+ - AtomicStampedReference :原子更新引用类型里的字段原子类
214287- AtomicMarkableReference :原子更新带有标记位的引用类型
215288
216289上面三个类提供的方法几乎相同,所以我们这里以 AtomicReference 为例子来介绍。
@@ -326,7 +399,7 @@ currentValue=0, currentStamp=0
326399currentValue=666, currentStamp=999, wCasResult=true
327400```
328401
329- #### 4.4 AtomicStampedReference 类使用示例
402+ #### 4.4 AtomicMarkableReference 类使用示例
330403
331404``` java
332405import java.util.concurrent.atomic.AtomicMarkableReference ;
0 commit comments