CAS algorithm and the usage of ABA
This article mainly introduces "CAS algorithm and the usage of ABA". In daily operation, I believe many people have doubts about the usage of CAS algorithm and ABA. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "CAS algorithm and ABA usage"! Next, please follow the editor to study!
Package com.shi.CAS;import java.util.concurrent.atomic.AtomicInteger;import java.util.concurrent.atomic.AtomicStampedReference;/** * ABA problem case * @ author shiye * * / public class ABATest1 {static AtomicInteger auAtomicInteger = new AtomicInteger; static AtomicStampedReference atomicStampedReference = new AtomicStampedReference (100 args 1); public static void main (String [] args) throws InterruptedException {System.out.println ("= generation of ABA problem =") New Thread (()-> {boolean A1 = auAtomicInteger.compareAndSet (100,200); System.out.println (Thread.currentThread (). GetName () + A1 + "the first modified value is" + auAtomicInteger.get ()); boolean a2 = auAtomicInteger.compareAndSet (200,100) System.out.println (Thread.currentThread (). GetName () + a2 + "the second modified value is" + auAtomicInteger.get ());}, "Thread A") .start () New Thread (()-> {try {Thread.sleep (1000);} catch (InterruptedException e) {e.printStackTrace () } boolean b1 = auAtomicInteger.compareAndSet (100,100,100,100,100); System.out.println (Thread.currentThread (). GetName () + b1 + "the first modified value is" + auAtomicInteger.get ();}, "thread B") .start () Thread.sleep (3000); System.out.println ("= solution of ABA problem ="); new Thread (()-> {int stamp1 = atomicStampedReference.getStamp ()) / / get the version System.out.println (Thread.currentThread (). GetName () + "current version is:" + stamp1); try {Thread.sleep (1000); / / Sleep 1s} catch (InterruptedException e) {e.printStackTrace () } boolean C1 = atomicStampedReference.compareAndSet (100,499, stamp1, stamp1+1); System.out.println (Thread.currentThread (). GetName () + C1 + "the value after the first modification is" + atomicStampedReference.getReference () + "version number is:" + atomicStampedReference.getStamp ()) Int stamp2 = atomicStampedReference.getStamp (); / / get version boolean C2 = atomicStampedReference.compareAndSet (499,100, stamp2, stamp2+1) System.out.println (Thread.currentThread (). GetName () + C2 + "the second modified value is" + atomicStampedReference.getReference () + "version number is:" + atomicStampedReference.getStamp ();}, "Thread C") .start () New Thread (()-> {int stamp1 = atomicStampedReference.getStamp (); / / get version System.out.println (Thread.currentThread (). GetName () + "current version is:" + stamp1); try {Thread.sleep (3000) / / Sleep 3s} catch (InterruptedException e) {e.printStackTrace ();} boolean D1 = atomicStampedReference.compareAndSet (100,2019, stamp1, stamp1+1) System.out.println (Thread.currentThread (). GetName () + D1 + "the first modified value is" + atomicStampedReference.getReference () + "version number is" + atomicStampedReference.getStamp ();}, "Thread D") .start ();}}
Execution result:
= generation of ABA problem = thread A true first modified value is 200 thread A true second modified value is 100 thread B true first modified value is 2019=ABA problem = thread C current version is: 1 thread D current version is: 1 thread C true the value after the first modification is version 499: 2 thread C false the value after the second modification is version 499 This number is: 2 thread D false the value after the first modification is 499 version number is: 2 to this The study on "CAS algorithm and the usage of ABA" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!