Java AtomicIntegerArray lazySet()方法及示例
Java.util.concurrent.atomic.AtomicIntegerArray.lazySet() 是Java中的一个内置方法,它最终在AtomicIntegerArray的任何给定索引上设置一个给定值。该方法将AtomicIntegerArray的索引值和要设置的值作为参数,并更新之前的值而不返回任何东西。
语法
public final void lazySet(int i, int newValue)
参数: 该函数需要两个参数。
- i是要进行更新的索引值。
- newValue是要在索引处更新的新值。
返回值: 该函数不返回任何值。
下面的程序说明了上述方法。
程序1 :
// Java program that demonstrates
// the lazySet() function
import java.util.concurrent.atomic.AtomicIntegerArray;
public class GFG {
public static void main(String args[])
{
// Initializing an array
int a[] = { 1, 2, 3, 4, 5 };
// Initializing an AtomicIntegerArray
// with array a
AtomicIntegerArray arr
= new AtomicIntegerArray(a);
// Displaying the AtomicIntegerArray
System.out.println("The array : " + arr);
// Index where operation is performed
int idx = 0;
// The new value to update at idx
int val = 10;
// Updating the value at
// idx applying lazySet
arr.lazySet(idx, val);
// Displaying the AtomicIntegerArray
System.out.println("The array after"
+ " update : "
+ arr);
}
}
输出。
The array : [1, 2, 3, 4, 5]
The array after update : [10, 2, 3, 4, 5]
程序2
// Java program that demonstrates
// the lazySet() function
import java.util.concurrent.atomic.AtomicIntegerArray;
public class GFG {
public static void main(String args[])
{
// Initializing an array
int a[] = { 1, 2, 3, 4, 5 };
// Initializing an AtomicIntegerArray
// with array a
AtomicIntegerArray arr
= new AtomicIntegerArray(a);
// Displaying the AtomicIntegerArray
System.out.println("The array : " + arr);
// Index where operation is performed
int idx = 3;
// The new value to update at idx
int val = 100;
// Updating the value at
// idx applying lazySet
arr.lazySet(idx, val);
// Displaying the AtomicIntegerArray
System.out.println("The array after "
+ "update : "
+ arr);
}
}
输出。
The array : [1, 2, 3, 4, 5]
The array after update : [1, 2, 3, 100, 5]
参考资料: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html#lazySet-int-int-
极客教程