举例1, CAS产生场景代码?
import java.util.concurrent.atomic.AtomicInteger; public class CASDemo { public static void main(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 class CAS_ABADemo { public static void main(String[] args) { //========================原子引用========================== User A = new User("A", 123); User B = new User("B", 456); //主物理内存 引用比较的是地址 AtomicReference<User> 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()); } } class User { String name; int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public User() { } public User(String name, int age) { this.name = name; this.age = age; } @Override public String 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 class ABADemo { static AtomicReference<Integer> atomicReference = new AtomicReference<>(100); static AtomicStampedReference<Integer> atomicStampedReference = new AtomicStampedReference<>(100, 1); public static void main(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(); } }