Java Concurrency 简明教程

AtomicReferenceArray Class

java.util.concurrent.atomic.AtomicReferenceArray 类在基础引用数组上提供操作,这些操作可以原子地进行读和写,还包含高级原子操作。AtomicReferenceArray 支持对基础引用数组变量进行原子操作。它有 get 和 set 方法,这些方法的工作方式类似于对 volatile 变量进行读取和写入。也就是说,set 与对同一变量的任何后续 get 具有存在先行关系。原子 compareAndSet 方法也具有这些内存一致性特性。

AtomicReferenceArray Methods

以下是 AtomicReferenceArray 类中可用一些重要方法的列表。

Sr.No.

Method & Description

1

public boolean compareAndSet(int i, E expect, E update) 如果当前值 == 预期值,则原子地将位置 i 处的元素设置为给定的更新值。

2

public E get(int i) 获得位置 i 的当前值。

3

public E getAndSet(int i, E newValue) 原子地将位置 i 处的元素设置为给定值,并返回旧值。

4

public void lazySet(int i, E newValue) 最终将位置 i 处的元素设置为给定值。

5

public int length() 返回数组的长度。

6

public void set(int i, E newValue) 将位置 i 处的元素设置为给定值。

7

public String toString() 返回数组当前值的字符串表示。

8

public boolean weakCompareAndSet(int i, E expect, E update) 如果当前值 == 预期值,则原子地将位置 i 处的元素设置为给定的更新值。

Example

以下 TestThread 程序展示了在基于线程的环境中,AtomicReferenceArray 变量的用法。

import java.util.concurrent.atomic.AtomicReferenceArray;

public class TestThread {
   private static String[] source = new String[10];
   private static AtomicReferenceArray<String> atomicReferenceArray
      = new AtomicReferenceArray<String>(source);

   public static void main(final String[] arguments) throws InterruptedException {

      for (int i = 0; i<atomicReferenceArray.length(); i++) {
         atomicReferenceArray.set(i, "item-2");
      }

      Thread t1 = new Thread(new Increment());
      Thread t2 = new Thread(new Compare());
      t1.start();
      t2.start();

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

   static class Increment implements Runnable {

      public void run() {

         for(int i = 0; i<atomicReferenceArray.length(); i++) {
            String add = atomicReferenceArray.getAndSet(i,"item-"+ (i+1));
            System.out.println("Thread " + Thread.currentThread().getId()
               + ", index " +i + ", value: "+ add);
         }
      }
   }

   static class Compare implements Runnable {

      public void run() {

         for(int i = 0; i<atomicReferenceArray.length(); i++) {
            System.out.println("Thread " + Thread.currentThread().getId()
               + ", index " +i + ", value: "+ atomicReferenceArray.get(i));
            boolean swapped = atomicReferenceArray.compareAndSet(i, "item-2", "updated-item-2");
            System.out.println("Item swapped: " + swapped);

            if(swapped) {
               System.out.println("Thread " + Thread.currentThread().getId()
                  + ", index " +i + ", updated-item-2");
            }
         }
      }
   }
}

这将产生以下结果。

Output

Thread 9, index 0, value: item-2
Thread 10, index 0, value: item-1
Item swapped: false
Thread 10, index 1, value: item-2
Item swapped: true
Thread 9, index 1, value: updated-item-2
Thread 10, index 1, updated-item-2
Thread 10, index 2, value: item-3
Item swapped: false
Thread 10, index 3, value: item-2
Item swapped: true
Thread 10, index 3, updated-item-2
Thread 10, index 4, value: item-2
Item swapped: true
Thread 10, index 4, updated-item-2
Thread 10, index 5, value: item-2
Item swapped: true
Thread 10, index 5, updated-item-2
Thread 10, index 6, value: item-2
Thread 9, index 2, value: item-2
Item swapped: true
Thread 9, index 3, value: updated-item-2
Thread 10, index 6, updated-item-2
Thread 10, index 7, value: item-2
Thread 9, index 4, value: updated-item-2
Item swapped: true
Thread 9, index 5, value: updated-item-2
Thread 10, index 7, updated-item-2
Thread 9, index 6, value: updated-item-2
Thread 10, index 8, value: item-2
Thread 9, index 7, value: updated-item-2
Item swapped: true
Thread 9, index 8, value: updated-item-2
Thread 10, index 8, updated-item-2
Thread 9, index 9, value: item-2
Thread 10, index 9, value: item-10
Item swapped: false