CAS-ABA问题编码实战

多线程情况下演示AtomicStampedReference解决ABA问题

package com.nanjing.gulimall.zhouyimo.test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @author zhou
 * @version 1.0
 * @date 2024/1/3 10:07 下午
 */
public class ABADemo {
    private static AtomicInteger atomicInteger = new AtomicInteger(100);

    private static AtomicStampedReference atomicStampedReference = new AtomicStampedReference<>(100,1);

    public static void main(String[] args) {
        new Thread(()->{
            int stamp = atomicStampedReference.getStamp();
            System.out.println(Thread.currentThread().getName()+"----"+"首次版本号:"+stamp);
            try {
                //暂停500毫秒,保证后面的t4线程初始化拿到的版本号和我一样
                TimeUnit.MICROSECONDS.sleep(500);
            } catch 

你可能感兴趣的:(JUC并发编程与源码分析,java,开发语言)