File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -191,7 +191,7 @@ MySQL默认是只write写入到page cache里面,然后再调用OS操作系统
191191 mysqlbinlog ‐‐no‐defaults ‐v ‐‐base64‐output= decode‐rows D:/ dev/ mysql‐5 .7 .25 ‐winx64/ data/ mysql‐binlog .000007
192192
193193 # 查看bin‐log二进制文件(带查询条件)
194- mysqlbinlog ‐‐no‐defaults ‐v ‐‐base64‐output= decode‐rows D:/ dev/ mysql‐5 .7 .25 ‐winx64/ data/ mysql‐binlog .000007 start‐datetime= " 2023‐01‐21 00:00:00" stop‐datetime= " 2023‐02‐01 00:00:00" start‐position= " 5000" stop‐position= " 20000"
194+ mysqlbinlog ‐‐no‐defaults ‐v ‐‐base64‐output= decode‐rows D:/ dev/ mysql‐5 .7 .25 ‐winx64/ data/ mysql‐binlog .000007 -- start‐datetime="2023‐01‐21 00:00:00" -- stop‐datetime="2023‐02‐01 00:00:00" -- start‐position="5000" -- stop‐position="20000"
195195 ```
196196
197197
Original file line number Diff line number Diff line change @@ -65,6 +65,37 @@ CAS存在ABA问题
6565
6666> 关心过程的案例,一个账户男女主同时在用,账户余额1万,男女主同时查询金额1万,女主取走1万消费,诈骗团伙转入1万,男主操作取款1万,ATM判断金额够,取款成功。
6767>
68- > 男主取款的1万是
69-
70- 解决方案,可以添加版本号,可使用AtomicStasmpedReference原子类。
68+ > 男主取款的1万是诈骗团伙转入的1万,而不是原来的1万。虽然最终余额都是1万,但资金来源已经发生了变化,这就是ABA问题的具体体现。
69+
70+ 解决方案:
71+
72+ 1 . ** 添加版本号** :在比较时不仅比较值,还比较版本号,确保操作的连续性。
73+
74+ 2 . ** 使用AtomicStampedReference原子类** :
75+ - 它维护了一个对象引用和一个整数标记(版本号)
76+ - 每次更新时,都会同时更新引用值和版本号
77+ - 比较时需要同时匹配引用值和版本号
78+
79+ 3 . ** 代码示例** :
80+ ``` java
81+ // 创建AtomicStampedReference,初始值为100,版本号为0
82+ AtomicStampedReference<Integer > atomicStampedRef = new AtomicStampedReference<> (100 , 0 );
83+
84+ // 获取当前值和版本号
85+ int currentValue = atomicStampedRef. getReference();
86+ int currentStamp = atomicStampedRef. getStamp();
87+
88+ // 尝试更新值为101,版本号加1
89+ // 只有当当前值为100且版本号为0时才会更新成功
90+ boolean success = atomicStampedRef. compareAndSet(
91+ currentValue, // 期望值
92+ 101 , // 新值
93+ currentStamp, // 期望版本号
94+ currentStamp + 1 // 新版本号
95+ );
96+ ```
97+
98+ 4 . ** 在银行账户案例中的应用** :
99+ - 每次账户余额变动时,版本号都递增
100+ - 男主取款时,不仅检查余额是否为1万,还要检查版本号是否与查询时一致
101+ - 这样就能防止ABA问题,确保操作的原子性和正确性
You can’t perform that action at this time.
0 commit comments