【Java】AtomicStampedReference解决CAS的ABA问题

AtomicStampReference

什么是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<Integer> 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=100

AtomicStampedReference解决ABA的问题

package com.keytech.task;

import java.util.concurrent.atomic.AtomicStampedReference;

public class CounterTest {

private AtomicStampedReference<Integer> count=new AtomicStampedReference<Integer>(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("只执行一次");

}

}

}

//只执行一次

【Java】AtomicStampedReference解决CAS的ABA问题

以上是 【Java】AtomicStampedReference解决CAS的ABA问题 的全部内容, 来源链接: utcz.com/a/91854.html

回到顶部