📌  相关文章
📜  Java中的 AtomicReferenceArray getAcquire() 方法及示例

📅  最后修改于: 2022-05-13 01:55:28.257000             🧑  作者: Mango

Java中的 AtomicReferenceArray getAcquire() 方法及示例

AtomicReferenceArray类的getAcquire()方法用于返回此 AtomicReferenceArray 对象的索引 i 处元素的值,其内存排序效果与 memory_order_acquire 排序兼容。

句法:

public final E getAcquire(int i)

参数:此方法接受索引 i以获取值。

返回值:此方法返回索引 i 处的当前值。

下面的程序说明了 getAcquire() 方法:
方案一:

// Java program to demonstrate
// AtomicReferenceArray.getAcquire() method
  
import java.util.concurrent.atomic.AtomicReferenceArray;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an atomic reference array
        // object which stores Integer.
        AtomicReferenceArray array
            = new AtomicReferenceArray(5);
  
        // set some value in array
        array.set(0, 1213);
        array.set(1, 1344);
        array.set(2, 1453);
        array.set(3, 1452);
  
        // get and print the value
        // using getAcquire method
        for (int i = 0; i < 4; i++) {
  
            int value = array.getAcquire(i);
            System.out.println("value at "
                               + i + " = " + value);
        }
    }
}
输出:

方案二:

// Java program to demonstrate
// AtomicReferenceArray.getAcquire() method
  
import java.util.concurrent.atomic.AtomicReferenceArray;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an atomic reference array object
        // which stores String.
        AtomicReferenceArray array
            = new AtomicReferenceArray(5);
  
        // set some value in array
        array.set(0, "GEEKS");
        array.set(1, "FOR");
        array.set(2, "GEEKS");
  
        // get and print the value using get method
        for (int i = 0; i < 2; i++) {
  
            String value = array.getAcquire(i);
            System.out.println("value at "
                               + i + " = " + value);
        }
    }
}
输出:

参考: https: Java/util/concurrent/atomic/AtomicReferenceArray.html#getAcquire(int)