Java AtomicLongArray get()方法及示例
Java.util.concurrent.atomic.AtomicLongArray.get() 是java中的一个内置方法,用于获取AtomicLongArray中任何位置的当前值。该方法以索引值为参数,并返回该索引的值。
语法
public final long get(int i)
参数: 该函数接受一个参数i,即要获取的索引值。
返回值: 该函数返回索引i处的当前值。
下面的程序说明了上述方法。
程序1 :
// Java program that demonstrates
// the get() function
import java.util.concurrent.atomic.AtomicLongArray;
public class GFG {
public static void main(String args[])
{
// Initializing an array
long a[] = { 1, 2, 3, 4, 5 };
// Initializing an AtomicLongArray with array a
AtomicLongArray arr = new AtomicLongArray(a);
// Displaying the AtomicLongArray
System.out.println("The array : " + arr);
// Index to get
int idx = 2;
// Using get() to retrieve value at idx
long val = arr.get(idx);
// Displaying the value at idx
System.out.println("Value at index " + idx
+ " is " + val);
}
}
输出。
The array : [1, 2, 3, 4, 5]
Value at index 2 is 3
程序2
// Java program that demonstrates
// the get() function
import java.util.concurrent.atomic.AtomicLongArray;
public class GFG {
public static void main(String args[])
{
// Initializing an array
long a[] = { 12, 22, 23, 24, 25 };
// Initializing an AtomicLongArray with array a
AtomicLongArray arr = new AtomicLongArray(a);
// Displaying the AtomicLongArray
System.out.println("The array : " + arr);
// Index to get
int idx = 4;
// Using get() to retrieve value at idx
long val = arr.get(idx);
// Displaying the value at idx
System.out.println("Value at index " + idx
+ " is " + val);
}
}
输出。
The array : [12, 22, 23, 24, 25]
Value at index 4 is 25
参考资料:
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicLongArray.html#get-int-