温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

CAS算法和ABA的用法

发布时间:2021-06-23 11:57:31 来源:亿速云 阅读:98 作者:chen 栏目:大数据

这篇文章主要介绍“CAS算法和ABA的用法”,在日常操作中,相信很多人在CAS算法和ABA的用法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”CAS算法和ABA的用法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

CAS算法和ABA的用法

CAS算法和ABA的用法

CAS算法和ABA的用法

CAS算法和ABA的用法

CAS算法和ABA的用法

CAS算法和ABA的用法

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

到此,关于“CAS算法和ABA的用法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

cas
AI