溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

CAS算法和ABA的用法

發(fā)布時(shí)間:2021-06-23 11:57:31 來源:億速云 閱讀:108 作者:chen 欄目:大數(shù)據(jù)

這篇文章主要介紹“CAS算法和ABA的用法”,在日常操作中,相信很多人在CAS算法和ABA的用法問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”CAS算法和ABA的用法”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

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問題的產(chǎn)生===============");
		
		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() + "當(dāng)前版本是: " + 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() + " 版本號(hào)為: " + atomicStampedReference.getStamp());
			
			int stamp2 = atomicStampedReference.getStamp();//獲取版本 
			boolean C2 = atomicStampedReference.compareAndSet(499, 100, stamp2, stamp2+1);
			System.out.println(Thread.currentThread().getName() + C2 + " 第二次  修改之后的值為 " + atomicStampedReference.getReference() + " 版本號(hào)為: " + atomicStampedReference.getStamp());
		
		},"線程C ").start();
		
		new Thread(()->{
			int stamp1 = atomicStampedReference.getStamp();//獲取版本 
			System.out.println(Thread.currentThread().getName() + "當(dāng)前版本是: " + 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() + " 版本號(hào)為: " + atomicStampedReference.getStamp());

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

}

執(zhí)行結(jié)果:

===============ABA問題的產(chǎn)生===============
線程A true 第一次  修改之后的值為 200
線程A true 第二次 修改之后的值為 100
線程B true 第一次 修改之后的值為 2019
===============ABA問題的解決===============
線程C 當(dāng)前版本是: 1
線程D 當(dāng)前版本是: 1
線程C true 第一次  修改之后的值為 499 版本號(hào)為: 2
線程C false 第二次  修改之后的值為 499 版本號(hào)為: 2
線程D false 第一次  修改之后的值為 499 版本號(hào)為: 2

到此,關(guān)于“CAS算法和ABA的用法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

cas
AI