AtomicReference 的原理

AtomicReference 的原理

This article will cover the AtomicReference and AtomicStampedReference properties of a Java concurrent API.

Atomicity

If you look up the meaning of atomic, you will get the answer ‘whole’. So an atomic operation is supposed to be completed wholly or not at all. Other threads will not be able to see the operation “in progress” as it will never be viewed in a partially completed state due to its CAS (Compare and Swap) principle, which is the one machine (CPU) instruction. The memory effects for accesses and updates of atomics generally follow the rules for volatiles happen-before conditions. So another advantage is ‘no context switching’ or one can say ‘Thread-Safe’.

AtomicReference

Like AtomicInteger, AtomicLong, etc. we can have an atomic object as well — AtomicReference, which provides various utility methods to perform atomic operations. Take an example:

package com.leonardoz.patterns.non_blocking;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @Auther: viagra
 * @Date: 2019/11/14 10:01
 * @Description:
 */
public class AtomicReferenceDemo {

    static AtomicReference<Person> p = new AtomicReference<Person>(new Person(20));

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i <= 3; i++) {
                    p.set(new Person(p.get().age + 10));
                    System.out.println("Atomic Check by first thread: " + Thread.currentThread().getName() + " is " + p.get().age);
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                Person per = p.get();
                for (int i = 1; i <= 3; i++) {
                    System.err.println(p.get().equals(per) + "_" + per.age + "_" + p.get().age);
                    p.compareAndSet(per, new Person(p.get().age + 10));
                    System.out.println("Atomic Check by second thread : " + Thread.currentThread().getName() + " is " + p.get().age);
                }
            }
        });
        t1.start();
        t2.start();
        // 等待线程的执行结果
        t1.join();
        t2.join();
        System.out.println("Final value: " + p.get().age);
    }


}
class Person {
    int age;

    public Person(int i) {
        age = i;
    }
}

Output

"C:\Program Files\Java\jdk1.8.0_101\bin\java.exe" "-javaagent:E:\software\JetBrains\IntelliJ IDEA 2019.1.2\IntelliJ IDEA 2018.3\lib\idea_rt.jar=5256:E:\software\JetBrains\IntelliJ IDEA 2019.1.2\IntelliJ IDEA 2018.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_101\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\rt.jar;D:\curator-workshop\java-concurrency-patterns2\target\classes" com.leonardoz.patterns.non_blocking.AtomicReferenceDemo
Atomic Check by first thread: Thread-0 is 30
Atomic Check by first thread: Thread-0 is 40
Atomic Check by first thread: Thread-0 is 50
true_50_50
Atomic Check by second thread : Thread-1 is 60
false_50_60
Atomic Check by second thread : Thread-1 is 60
false_50_60
Atomic Check by second thread : Thread-1 is 60
Final value: 60

Process finished with exit code 0

注释t1.join()和注释t2.join()的结果如下:

AtomicReference 的原理_第1张图片

As you see, how compareAndSet had worked here. It first checks whether the desired object matches an existing object or not, whereas a set method forcefully overrides the existing object. In the above example, the second thread is able to get matched with the existing object and when it could not, then it did not set the new value.

AtomicStampedReference

It adds one more fields to its constructor, which is the stamp. This stamp helps developers check whether any other thread trespassed the section and exited after making changes. It is a well-known A-B-A problem. The A-B-A problem is when a reference is changed from pointing to A, then to B, and then back to A. In the example below, the stamp value is being communicated between two threads.

import java.util.concurrent.atomic.AtomicStampedReference;
public class AtomicRefEg {
    static int stampVal = 1;
    static AtomicStampedReference<Person> s  = new AtomicStampedReference<Person>(new Person(20), stampVal);
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable(){
            @Override
            public void run() {
                for(int i=1 ; i<=3 ; i++) {
                    System.out.println("stamp value for first thread:"+stampVal);
                    s.compareAndSet(s.getReference(), new Person(s.getReference().age+10), stampVal, ++stampVal);
                    System.out.println("Atomic Check by first thread: "+Thread.currentThread().getName()+" is "+s.getReference().age);
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=1 ; i<=3 ; i++){
                    System.out.println("stamp value for second thread:"+stampVal);
                    s.compareAndSet(s.getReference(), new Person(s.getReference().age+10), stampVal, ++stampVal);
                    System.out.println("Atomic Check by second thread : "+Thread.currentThread().getName()+" is "+s.getReference().age);
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("Final value: "+s.getReference().age);
    }
}
class Person {
    int age;
    public Person(int i) {
        age=i;
    }
}

output

AtomicReference 的原理_第2张图片

你可能感兴趣的:(java,多线程)