import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
/** * @description: ABA问题的产生代码示例 * @author: xz */
public class ABADemo {
private static AtomicReference<Integer> atomicReference=new AtomicReference<>(100);
public static void main(String[] args) {
System.out.println("===以下是ABA问题的产生===");
new Thread(()->{
atomicReference.compareAndSet(100,101);
atomicReference.compareAndSet(101,100);
},"t1").start();
new Thread(()->{
//先暂停1秒 保证完成ABA
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
System.out.println(atomicReference.compareAndSet(100, 2019)+"\t"+atomicReference.get());
},"t2").start();
}
}
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.concurrent.atomic.AtomicReference;
/** * @description: 原子引用代码示例 * @author: xz */
@Getter
@Setter
@AllArgsConstructor
@ToString
class User{
private String name;
private int age;
}
public class AtomicReferenceDemo {
public static void main(String[] args) {
User zs = new User("张三", 29);
User ls = new User("李四", 22);
//AtomicReference中的泛型User表示原子引用类
AtomicReference<User> userAtomicReference = new AtomicReference<>();
userAtomicReference.set(zs);
//当期望的引用对象zs和主内存中的引用对象zs相同,修改为更新的ls引用对象
System.out.println(userAtomicReference.compareAndSet(zs, ls)+"\t"+userAtomicReference.get().toString());
//当期望的引用对象zs和主内存中的引用对象zs相同,修改为更新的ls引用对象
System.out.println(userAtomicReference.compareAndSet(zs, ls)+"\t"+userAtomicReference.get().toString());
}
}
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicStampedReference;
/** * @description: 通过AtomicStampedReference 时间戳原子引用类 解决ABA问题 * @author: xz */
public class ABADemo {
private static AtomicStampedReference<Integer> stampedReference=new AtomicStampedReference<>(100,1);
public static void main(String[] args) {
System.out.println("===以下是ABA问题的解决===");
new Thread(()->{
int stamp = stampedReference.getStamp();
System.out.println(Thread.currentThread().getName()+
"\t 第1次版本号"+stamp+"\t值是"+stampedReference.getReference());
//暂停1秒钟t1线程
try { TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
stampedReference.compareAndSet(100,101,stampedReference.getStamp(),stampedReference.getStamp()+1);
System.out.println(Thread.currentThread().getName()+"\t 第2次版本号"+stampedReference.getStamp()+"\t值是"+stampedReference.getReference());
stampedReference.compareAndSet(101,100,stampedReference.getStamp(),stampedReference.getStamp()+1);
System.out.println(Thread.currentThread().getName()+"\t 第3次版本号"+stampedReference.getStamp()+"\t值是"+stampedReference.getReference());
},"t1").start();
new Thread(()->{
int stamp = stampedReference.getStamp();
System.out.println(Thread.currentThread().getName()+
"\t 第1次版本号"+stamp+"\t值是"+stampedReference.getReference());
//保证线程t1完成1次ABA
try { TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
boolean result = stampedReference.compareAndSet(100, 2019, stamp, stamp + 1);
System.out.println(Thread.currentThread().getName()+"\t 修改成功否"+result+"\t最新版本号"+stampedReference.getStamp());
System.out.println("最新的值\t"+stampedReference.getReference());
},"t2").start();
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://wwwxz.blog.csdn.net/article/details/122420739
内容来源于网络,如有侵权,请联系作者删除!