Java 原子性引用 AtomicReference

更多 Java 并發編程方面的文章,請參見文集《Java 并發編程》


AtomicReference

An object reference that may be updated atomically.

The AtomicReference class provides reference objects that may be read and written atomically, so when multiple threads try to reach them at the same time, only one will be able to do so.
提供了引用變量的讀寫原子性操作。

提供了如下的方法:

  • compareAndSet(V expect, V update):Atomically sets the value to the given updated value if the current value == the expected value.
  • getAndSet(V newValue):Atomically sets to the given value and returns the old value.
  • lazySet(V newValue):Eventually sets to the given value.
  • set(V newValue):Sets to the given value.
  • get():Gets the current value.

假設有一個類 Person,定義如下:

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = 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 String toString() {
        return "[name: " + this.name + ", age: " + this.age + "]";
    }
}

如果使用普通的對象引用,在多線程情況下進行對象的更新可能會導致不一致性。例如:
一個對象的初始狀態為 name=Tom, age = 18
在 線程1 中將 name 修改為 Tom1age + 1
在 線程2 中將 name 修改為 Tom2age + 2

我們認為只會產生兩種結果:

  • 若 線程1 先執行,線程2 后執行,則中間狀態為 name=Tom1, age = 19,結果狀態為 name=Tom2, age = 21
  • 若 線程2 先執行,線程1 后執行,則中間狀態為 name=Tom2, age = 20,結果狀態為 name=Tom1, age = 21

但是可能的輸出如下:

Person is [name: Tom, age: 18]
Thread2 Values [name: Tom1, age: 21]
Thread1 Values [name: Tom1, age: 21]
Now Person is [name: Tom1, age: 21]

// 普通引用
private static Person person;

public static void main(String[] args) throws InterruptedException {
    person = new Person("Tom", 18);

    System.out.println("Person is " + person.toString());

    Thread t1 = new Thread(new Task1());
    Thread t2 = new Thread(new Task2());

    t1.start();
    t2.start();

    t1.join();
    t2.join();

    System.out.println("Now Person is " + person.toString());
}

static class Task1 implements Runnable {
    public void run() {
        person.setAge(person.getAge() + 1);
        person.setName("Tom1");

        System.out.println("Thread1 Values "
                + person.toString());
    }
}

static class Task2 implements Runnable {
    public void run() {
        person.setAge(person.getAge() + 2);
        person.setName("Tom2");

        System.out.println("Thread2 Values "
                + person.toString());
    }
}

如果使用原子性對象引用,在多線程情況下進行對象的更新可以確保一致性。例如:

// 普通引用
private static Person person;
// 原子性引用
private static AtomicReference<Person> aRperson;

public static void main(String[] args) throws InterruptedException {
    person = new Person("Tom", 18);
    aRperson = new AtomicReference<Person>(person);

    System.out.println("Atomic Person is " + aRperson.get().toString());

    Thread t1 = new Thread(new Task1());
    Thread t2 = new Thread(new Task2());

    t1.start();
    t2.start();

    t1.join();
    t2.join();

    System.out.println("Now Atomic Person is " + aRperson.get().toString());
}

static class Task1 implements Runnable {
    public void run() {
        aRperson.getAndSet(new Person("Tom1", aRperson.get().getAge() + 1));

        System.out.println("Thread1 Atomic References "
                + aRperson.get().toString());
    }
}

static class Task2 implements Runnable {
    public void run() {
        aRperson.getAndSet(new Person("Tom2", aRperson.get().getAge() + 2));

        System.out.println("Thread2 Atomic References "
                + aRperson.get().toString());
    }
}

但是可能的輸出如下:

Atomic Person is [name: Tom, age: 18]
Thread1 Atomic References [name: Tom1, age: 19]
Thread2 Atomic References [name: Tom2, age: 21]
Now Atomic Person is [name: Tom2, age: 21]


引用:
Java AtomicReference Example

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容