java解决aba_AtomicStampedReference解决CAS的ABA问题

office vba开发经典中级进阶卷办公

79.2元

包邮

(需用券)

去购买 >

d3cf8777d93ded0c7a363cd37d1917ae.png

AtomicStampReference解决CAS的ABA问题

什么是ABAABA问题:指CAS操作的时候,线程将某个变量值由A修改为B,但是又改回了A,其他线程发现A并未改变,于是CAS将进行值交换操作,实际上该值已经被改变过,这与CAS的核心思想是不符合的

ABA解决方案每次变量更新的时候,把变量的版本号进行更新,如果某变量被某个线程修改过,那么版本号一定会递增更新,从而解决ABA问题

AtomicReference 演示ABA问题package com.keytech.task;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.atomic.AtomicInteger;

import java.util.concurrent.atomic.AtomicReference;

public class AtomicIntegerTest {

private static AtomicReference count=new AtomicReference<>(10);

public static void main(String[] args) {

ExecutorService executorService = Executors.newCachedThreadPool();

executorService.execute(()->{

boolean b = count.compareAndSet(10, 12);

if(b){

System.out.println(Thread.currentThread().getName()+"修改成功count="+count.get());

}

boolean c =count.compareAndSet(12, 10);

if(c){

System.out.println(Thread.currentThread().getName()+"修改成功count="+count.get());

}

});

executorService.execute(()->{

boolean b = count.compareAndSet(10, 100);

if(b){

System.out.println(Thread.currentThread().getName()+"修改成功count="+count.get());

}

});

executorService.shutdown();

}

}

//pool-1-thread-1修改成功count=12

//pool-1-thread-1修改成功count=10

//pool-1-thread-2修改成功count=100pool-1-thread-1将count由10修改成12,又将count从12改成10。 pool-1-thread-2将count从10成功改成100。出现了ABA的问题。

AtomicStampedReference解决ABA的问题以计数器的实现为例,计数器通常用来统计在线人数,在线+1,离线-1,是ABA的典型场景。package com.keytech.task;

import java.util.concurrent.atomic.AtomicStampedReference;

public class CounterTest {

private AtomicStampedReference count=new AtomicStampedReference(0,0);

public int getCount(){

return count.getReference();

}

public int increment(){

int[] stamp=new int[1];

while (true){

Integer value = count.get(stamp);

int newValue=value+1;

boolean b = count.compareAndSet(value, newValue, stamp[0], stamp[0] + 1);

if(b){

return newValue;

}

}

}

public int decrement(){

int[] stamp=new int[1];

while(true){

Integer value=count.get(stamp);

int newValue=value-1;

boolean b = count.compareAndSet(value, newValue, stamp[0], stamp[0] + 1);

if(b){

return newValue;

}

}

}

}

调用计数器package com.keytech.task;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Semaphore;

import java.util.concurrent.atomic.AtomicInteger;

import java.util.concurrent.atomic.AtomicReference;

public class AtomicIntegerTest {

public static void main(String[] args) {

ExecutorService executorService = Executors.newCachedThreadPool();

Semaphore semaphore=new Semaphore(200);

CounterTest counterTest=new CounterTest();

for (int i = 0; i < 5000; i++) {

executorService.execute(()->{

try{

semaphore.acquire();

counterTest.increment();

semaphore.release();

}catch (Exception e){

e.printStackTrace();

}

});

executorService.execute(()->{

try{

semaphore.acquire();

counterTest.decrement();

semaphore.release();

}catch (Exception e){

e.printStackTrace();

}

});

}

executorService.shutdown();

System.out.println(counterTest.getCount());

}

}

//输出0

AtomicBoolean保证高并发下只执行一次package com.keytech.task;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Semaphore;

import java.util.concurrent.atomic.AtomicBoolean;

public class AtomicBooleanTest {

private static AtomicBoolean isHappen=new AtomicBoolean(false);

public static int clientTotal=5000;

public static int threadTotal=200;

public static void main(String[] args) {

ExecutorService executorService = Executors.newCachedThreadPool();

Semaphore semaphore=new Semaphore(threadTotal);

for (int i = 0; i < clientTotal; i++) {

executorService.execute(()->{

try {

semaphore.acquire();

update();

semaphore.release();

}catch (Exception e){

e.printStackTrace();

}

});

}

executorService.shutdown();

}

private static void update(){

if(isHappen.compareAndSet(false, true)){

System.out.println("只执行一次");

}

}

}

//只执行一次

1609725230700054.jpg

java 11官方入门(第8版)教材

79.84元

包邮

(需用券)

去购买 >

f0f3f55624fb396b1764d42d6df88864.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值