Defects and solutions of CAS

Keywords: Multithreading cas

1, ABA problem

ABA problem means that in a concurrent environment, assuming that the initial condition is a, when modifying data, it will be modified if it is found to be a. However, although we see a, there may be a change from a to B and B back to a. This a is not that A. even if the data is successfully modified, there may be problems

Solution: add the version number through AtomicStampedReference. Even if the value is the same, it can be identified by the version number

public class Main {
    public static void main(String[] args) {
        AtomicStampedReference<String> atomicStampedReference = new AtomicStampedReference("tom", 10);
        int oldStamp = atomicStampedReference.getStamp();
        String oldName = atomicStampedReference.getReference();

        System.out.println("Version after initialization:" + oldStamp);
        System.out.println("Value after initialization:" + oldName);

        String newName = "jerry";

        if (!atomicStampedReference.compareAndSet(oldName, newName, 1, oldStamp + 1)) {
            System.out.println("The version is inconsistent and cannot be modified Reference Value of");
        if (atomicStampedReference.compareAndSet(oldName, newName, oldStamp, oldStamp + 1)) {
            System.out.println("Consistent version, modified reference The value of is jerry");
        System.out.println("Version after modification:" + atomicStampedReference.getStamp());
        System.out.println("Value after modification:" + atomicStampedReference.getReference());
Version after initialization: 10
 Value after initialization: tom
 The version is inconsistent and cannot be modified Reference Value of
 Consistent version, modified reference The value of is jerry
 Version after successful modification: 11
 Value after modification: jerry

  2, Spin time too long

If the conditions are not met, CAS will continue to spin, wasting CPU resources

Solution: limit the number of spins and adopt adaptive spin

Spin lock and adaptive spin_ The biggest impact of mutually exclusive synchronization of KKKL blog CSDN blog on performance is the implementation of blocking. The operations of suspending threads and restoring threads need to be completed in the kernel state. These operations bring great pressure to the concurrency performance of Java virtual machine. At the same time, the virtual machine development team also noticed that in many applications, the locking state of shared data will only last for a short period of time. It is not worth suspending and restoring threads for this period of time. At present, most personal computers and servers are multi-channel (core) processor systems. If the physical machine has more than one processor or processor core, which can make two or more threads execute in parallel at the same time, we can make the thread requesting lock "wait a moment", but do not give up the execution time of the processor, See if the thread holding the lock will release the lock soon. To make

3, Only the atomicity of one variable operation can be guaranteed

Ordinary CAS can only compare one value. If you want to modify an object atomically, and there are multiple attributes in the object, atomicity cannot be guaranteed

Solution: implemented through AtomicReference

public class Main {
    public static void main(String[] args) {
        AtomicReference<User> atomicUserRef = new AtomicReference<>();
        User user = new User("name1", 1);
        User updateUser = new User("name2", 2);
        atomicUserRef.compareAndSet(user, updateUser);

    static class User {
        public String name;
        public int old;

        public User(String name, int old) {
   = name;
            this.old = old;

Posted by ashok_bam on Thu, 28 Oct 2021 09:18:38 -0700