前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CAS算法和ABA问题

CAS算法和ABA问题

作者头像
用户5927264
发布2019-09-11 17:17:15
4270
发布2019-09-11 17:17:15
举报
文章被收录于专栏:OSChinaOSChinaOSChina
package com.shi.CAS;

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

/**
 * ABA问题案例
 * @author shiye
 *
 */
public class ABATest1 {
	static AtomicInteger auAtomicInteger = new AtomicInteger(100);
	static AtomicStampedReference<Integer> atomicStampedReference = new AtomicStampedReference<>(100,1);

	public static void main(String[] args) throws InterruptedException {
		System.out.println("===============ABA问题的产生===============");
		
		new Thread(()-> {
			boolean a1 = auAtomicInteger.compareAndSet(100, 200);
			System.out.println(Thread.currentThread().getName() + a1 + " 第一次  修改之后的值为 " + auAtomicInteger.get());
			boolean a2 = auAtomicInteger.compareAndSet(200, 100);
			System.out.println(Thread.currentThread().getName() + a2 + " 第二次 修改之后的值为 " + auAtomicInteger.get());
		},"线程A ").start();
		
		new Thread(()-> {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			boolean b1 = auAtomicInteger.compareAndSet(100, 2019);
			System.out.println(Thread.currentThread().getName() + b1 + " 第一次 修改之后的值为 " + auAtomicInteger.get());
		},"线程B ").start();
		
		Thread.sleep(3000);
		System.out.println("===============ABA问题的解决===============");
		
		new Thread(()->{
			int stamp1 = atomicStampedReference.getStamp();//获取版本 
			System.out.println(Thread.currentThread().getName() + "当前版本是: " + stamp1);
			try {
				Thread.sleep(1000);//睡眠1s
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			boolean C1 = atomicStampedReference.compareAndSet(100, 499, stamp1, stamp1+1);
			System.out.println(Thread.currentThread().getName() + C1 + " 第一次  修改之后的值为 " + atomicStampedReference.getReference() + " 版本号为: " + atomicStampedReference.getStamp());
			
			int stamp2 = atomicStampedReference.getStamp();//获取版本 
			boolean C2 = atomicStampedReference.compareAndSet(499, 100, stamp2, stamp2+1);
			System.out.println(Thread.currentThread().getName() + C2 + " 第二次  修改之后的值为 " + atomicStampedReference.getReference() + " 版本号为: " + atomicStampedReference.getStamp());
		
		},"线程C ").start();
		
		new Thread(()->{
			int stamp1 = atomicStampedReference.getStamp();//获取版本 
			System.out.println(Thread.currentThread().getName() + "当前版本是: " + stamp1);
			try {
				Thread.sleep(3000);//睡眠3s
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			boolean d1 = atomicStampedReference.compareAndSet(100, 2019, stamp1, stamp1+1);
			System.out.println(Thread.currentThread().getName() + d1 + " 第一次  修改之后的值为 " + atomicStampedReference.getReference() + " 版本号为: " + atomicStampedReference.getStamp());

		},"线程D ").start();
	}

}

执行结果:

===============ABA问题的产生===============
线程A true 第一次  修改之后的值为 200
线程A true 第二次 修改之后的值为 100
线程B true 第一次 修改之后的值为 2019
===============ABA问题的解决===============
线程C 当前版本是: 1
线程D 当前版本是: 1
线程C true 第一次  修改之后的值为 499 版本号为: 2
线程C false 第二次  修改之后的值为 499 版本号为: 2
线程D false 第一次  修改之后的值为 499 版本号为: 2
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档