Java Concurrency 简明教程

Java Concurrency - AtomicIntegerArray Class

java.util.concurrent.atomic.AtomicIntegerArray 类提供对基础 int 数组的操作,该数组可被原子性地读写,并且还包含高级原子操作。AtomicIntegerArray 支持对基础 int 数组变量的原子操作。它有类似对 volatile 变量的读写操作的 get 和 set 方法。也就是说,set 操作与任何对相同变量的后续 get 操作之间存在先行发生关系。原子 compareAndSet 方法也有这些内存一致性特征。

A java.util.concurrent.atomic.AtomicIntegerArray class provides operations on underlying int array that can be read and written atomically, and also contains advanced atomic operations. AtomicIntegerArray supports atomic operations on underlying int array variable. It have get and set methods that work like reads and writes on volatile variables. That is, a set has a happens-before relationship with any subsequent get on the same variable. The atomic compareAndSet method also has these memory consistency features.

AtomicIntegerArray Methods

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

Following is the list of important methods available in the AtomicIntegerArray class.

Sr.No.

Method & Description

1

public int addAndGet(int i, int delta) Atomically adds the given value to the element at index i.

2

public boolean compareAndSet(int i, int expect, int update) Atomically sets the element at position i to the given updated value if the current value == the expected value.

3

public int decrementAndGet(int i) Atomically decrements by one the element at index i.

4

public int get(int i) Gets the current value at position i.

5

public int getAndAdd(int i, int delta) Atomically adds the given value to the element at index i.

6

public int getAndDecrement(int i) Atomically decrements by one the element at index i.

7

public int getAndIncrement(int i) Atomically increments by one the element at index i.

8

public int getAndSet(int i, int newValue) Atomically sets the element at position i to the given value and returns the old value.

9

public int incrementAndGet(int i) Atomically increments by one the element at index i.

10

public void lazySet(int i, int newValue) Eventually sets the element at position i to the given value.

11

public int length() Returns the length of the array.

12

public void set(int i, int newValue) Sets the element at position i to the given value.

13

public String toString() Returns the String representation of the current values of array.

14

public boolean weakCompareAndSet(int i, int expect, int update) Atomically sets the element at position i to the given updated value if the current value == the expected value.

Example

以下 TestThread 程序演示了在基于线程的环境中使用 AtomicIntegerArray 变量。

The following TestThread program shows usage of AtomicIntegerArray variable in thread based environment.

import java.util.concurrent.atomic.AtomicIntegerArray;

public class TestThread {
   private static AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(10);

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

      for (int i = 0; i<atomicIntegerArray.length(); i++) {
         atomicIntegerArray.set(i, 1);
      }

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

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

      System.out.println("Values: ");

      for (int i = 0; i<atomicIntegerArray.length(); i++) {
         System.out.print(atomicIntegerArray.get(i) + " ");
      }
   }

   static class Increment implements Runnable {

      public void run() {

         for(int i = 0; i<atomicIntegerArray.length(); i++) {
            int add = atomicIntegerArray.incrementAndGet(i);
            System.out.println("Thread " + Thread.currentThread().getId()
               + ", index " +i + ", value: "+ add);
         }
      }
   }

   static class Compare implements Runnable {

      public void run() {

         for(int i = 0; i<atomicIntegerArray.length(); i++) {
            boolean swapped = atomicIntegerArray.compareAndSet(i, 2, 3);

            if(swapped) {
               System.out.println("Thread " + Thread.currentThread().getId()
                  + ", index " +i + ", value: 3");
            }
         }
      }
   }
}

这将产生以下结果。

This will produce the following result.

Output

Thread 10, index 0, value: 2
Thread 10, index 1, value: 2
Thread 10, index 2, value: 2
Thread 11, index 0, value: 3
Thread 10, index 3, value: 2
Thread 11, index 1, value: 3
Thread 11, index 2, value: 3
Thread 10, index 4, value: 2
Thread 11, index 3, value: 3
Thread 10, index 5, value: 2
Thread 10, index 6, value: 2
Thread 11, index 4, value: 3
Thread 10, index 7, value: 2
Thread 11, index 5, value: 3
Thread 10, index 8, value: 2
Thread 11, index 6, value: 3
Thread 10, index 9, value: 2
Thread 11, index 7, value: 3
Thread 11, index 8, value: 3
Thread 11, index 9, value: 3
Values:
3 3 3 3 3 3 3 3 3 3