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

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

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

AtomicReferenceArray类的getPlain()方法用于返回此 AtomicReferenceArray 对象的索引 i 处的元素的值,其内存语义读取就像变量被声明为非易失性一样。
句法:

public final E getPlain(int i)

参数:此方法接受索引i以获取值。
返回值:此方法返回索引 i 处的当前值
下面的程序说明了 getPlain() 方法:
方案一:

Java
// Java program to demonstrate
// AtomicReferenceArray.getPlain() method
  
import java.util.concurrent.atomic.*;
  
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, 987);
        array.set(1, 988);
        array.set(2, 989);
        array.set(3, 986);
  
        // get and print the value
        // using getPlain method
        for (int i = 0; i < 4; i++) {
  
            int value = array.getPlain(i);
            System.out.println("value at "
                               + i + " = "
                               + value);
        }
    }
}


Java
// Java program to demonstrate
// AtomicReferenceArray.getPlain() method
  
import java.util.concurrent.atomic.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a array of Strings
        String[] names
            = { "AMAN", "AMAR", "SURAJ" };
  
        // create an atomic reference object.
        AtomicReferenceArray array
            = new AtomicReferenceArray(names);
  
        // get and print the value
        // using getPlain method
        for (int i = 0; i < array.length(); i++) {
  
            String value = array.getPlain(i);
            System.out.println("value at "
                               + i + " = "
                               + value);
        }
    }
}


输出:
value at 0 = 987
value at 1 = 988
value at 2 = 989
value at 3 = 986

方案二:

Java

// Java program to demonstrate
// AtomicReferenceArray.getPlain() method
  
import java.util.concurrent.atomic.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a array of Strings
        String[] names
            = { "AMAN", "AMAR", "SURAJ" };
  
        // create an atomic reference object.
        AtomicReferenceArray array
            = new AtomicReferenceArray(names);
  
        // get and print the value
        // using getPlain method
        for (int i = 0; i < array.length(); i++) {
  
            String value = array.getPlain(i);
            System.out.println("value at "
                               + i + " = "
                               + value);
        }
    }
}
输出:
value at 0 = AMAN
value at 1 = AMAR
value at 2 = SURAJ

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