原子引用原子引用解决ABA问题，解决思路：乐观锁package com.sw.cas; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicStampedReference; /** * @Author suaxi * @Date 2021/2/21 21:29 * 原子引用解决ABA问题 */ public class casTest01 { static AtomicStampedReference<Integer> atomicStampedReference = new AtomicStampedReference<>(5, 1); public static void main(String[] args) { new Thread(() ->{ int stamp = atomicStampedReference.getStamp(); System.out.println("A1--->"+stamp); try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(atomicStampedReference.compareAndSet(5, 6, atomicStampedReference.getStamp(), atomicStampedReference.getStamp() + 1)); System.out.println("A2--->"+atomicStampedReference.getStamp()); System.out.println(atomicStampedReference.compareAndSet(6, 5, atomicStampedReference.getStamp(), atomicStampedReference.getStamp() + 1)); System.out.println("A3--->"+atomicStampedReference.getStamp()); },"A").start(); new Thread(() ->{ int stamp = atomicStampedReference.getStamp(); System.out.println("B1--->"+stamp); try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(atomicStampedReference.compareAndSet(5, 10, stamp, stamp + 1)); System.out.println("B2--->"+atomicStampedReference.getStamp()); },"B").start(); } } Integer包装类补充：