Java 并发 AtomicReferenceArray类


java.util.concurrent.atomic.AtomicReferenceArray类提供对基础参考数组的操作,该数组可以以原子方式读取和写入,并且还包含高级原子操作。AtomicReferenceArray支持底层引用数组变量的原子操作。它具有读取和写入易失性变量的方法。也就是说,一个集合与之后的任何一个变量之间都有一个前后关系。原子compareAndSet方法也具有这些内存一致性功能。

AtomicReferenceArray方法

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

Sr.No. 方法和描述
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上的元素设置为给定的更新值。

以下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");
            }
         }
      }
   }
}

这将产生以下结果。

输出

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