当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java AtomicReferenceArray getPlain()用法及代码示例


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

用法:

public final E getPlain(int i)

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


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

以下示例程序旨在说明getPlain()方法:
程序1:

// 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<Integer> array 
            = new AtomicReferenceArray<Integer>(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); 
        } 
    } 
}
输出:

程序2:

// 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<String> array 
            = new AtomicReferenceArray<String>(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); 
        } 
    } 
}
输出:

引用:



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 AtomicReferenceArray getPlain() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。