CAS你知道吗?原子类AtomicInteger的ABA问题谈谈?,原子共面问题(1)CAS是什么?
比较并交换
举例1, CAS产生场景代码?
importjava.util.concurrent.atomic.AtomicInteger;public classCASDemo {public static voidmain(String[] args) {
AtomicInteger atomicInteger=new AtomicInteger(5);//主物理内存//比较5
System.out.println(atomicInteger.compareAndSet(5, 2019)+"\t current data(主物理内存) : "+atomicInteger.get());//修改失败
System.out.println(atomicInteger.compareAndSet(5, 1024)+"\t current data(主物理内存) : "+atomicInteger.get());
}
}
举例2, CAS产生场景代码?原子引用
public classCAS_ABADemo {public static voidmain(String[] args) {//========================原子引用==========================
User A = new User("A", 123);
User B= new User("B", 456);//主物理内存 引用比较的是地址
AtomicReference atomicReference1 = new AtomicReference<>();
atomicReference1.set(A);
System.out.println(atomicReference1.compareAndSet(A, B)+ "\t" +atomicReference1.get().toString());
System.out.println(atomicReference1.compareAndSet(A, B)+ "\t" +atomicReference1.get().toString());
}
}classUser {
String name;intage;publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicUser() {
}public User(String name, intage) {this.name =name;this.age =age;
}
@OverridepublicString toString() {return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
原子引用
参考博客:CopyOnWriteArrayList 写时复制----------https://www.cnblogs.com/huangjuncong/p/9160713.html
参考博客:ReentrantLock 独占锁(可重入锁)----------https://www.cnblogs.com/takumicx/p/9338983.html
举例3,ABA问题的产生和解决方案代码场景。
public classABADemo {static AtomicReference atomicReference = new AtomicReference<>(100);static AtomicStampedReference atomicStampedReference = new AtomicStampedReference<>(100, 1);public static voidmain(String[] args) {
System.out.println("======ABA问题的产生======");new Thread(() ->{
atomicReference.compareAndSet(100, 101);
atomicReference.compareAndSet(101, 100);
},"t1").start();new Thread(() ->{try{
TimeUnit.SECONDS.sleep(1);
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println(atomicReference.compareAndSet(100, 2019) + "\t" +atomicReference.get().toString());
},"t2").start();try { TimeUnit.SECONDS.sleep(2); } catch(InterruptedException e) { e.printStackTrace(); }
System.out.println("======ABA问题的解决======");new Thread(() ->{int stamp =atomicStampedReference.getStamp();
System.out.println(Thread.currentThread().getName()+ "\t第一次版本号: " +stamp);try { TimeUnit.SECONDS.sleep(1); } catch(InterruptedException e) { e.printStackTrace(); }
atomicStampedReference.compareAndSet(100,101,
atomicStampedReference.getStamp(),atomicStampedReference.getStamp()+1);
System.out.println(Thread.currentThread().getName()+ "\t第二次版本号: " +atomicStampedReference.getStamp());
atomicStampedReference.compareAndSet(101,100,
atomicStampedReference.getStamp(),atomicStampedReference.getStamp()+1);
System.out.println(Thread.currentThread().getName()+ "\t第三次版本号: " +atomicStampedReference.getStamp());
},"t3").start();new Thread(() ->{int stamp =atomicStampedReference.getStamp();
System.out.println(Thread.currentThread().getName()+ "\t第一次版本号: " +stamp);try { TimeUnit.SECONDS.sleep(3); } catch(InterruptedException e) { e.printStackTrace(); }boolean result=atomicStampedReference.compareAndSet(100,2019,
stamp,stamp+1);
System.out.println(Thread.currentThread().getName()+"\t修改成功与否:"+result+" 当前最新版本号"+atomicStampedReference.getStamp());
System.out.println(Thread.currentThread().getName()+"\t当前实际值:"+atomicStampedReference.getReference());
},"t4").start();
}
}
ABA问题的产生和解决方案
(3)CAS实现原子操作的三大问题。
CAS虽然采用自旋的方式高效的解决了原子操作,但任然存在三个问题.
相关文章暂无相关文章