Java.util.concurrent.atomic.AtomicIntegerArray.getAndSet()是Java中的一种内置方法,该方法原子地在AtomicIntegerArray的任何给定位置设置给定值。该方法采用AtomicIntegerArray的索引值和要设置为参数的值。在给定索引处设置新值之前,它将返回给定索引处的值。函数getAndSet()与set()函数相似,但前者返回值,而后者不返回任何值。
用法:
public final int getAndSet(int i, int newValue)
参数:该函数接受两个参数:
- i–要进行更新的索引。
- newValue–在索引i处设置的值
返回值:函数以整数形式返回更新前给定索引处的值。
以下示例程序旨在说明上述方法:
示例1:
// Java program that demonstrates
// the getAndSet() function
import java.util.concurrent.atomic.AtomicIntegerArray;
public class GFG {
public static void main(String args[])
{
// Initializing an array
int a[] = { 11, 12, 13, 14, 15 };
// 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;
// New value to set at idx
int val = 100;
// Updating the value at
// idx applying getAndSet
// and store previous value
int prev = arr.getAndSet(idx, val);
// Previous value at idx before update
System.out.println("Value at index " + idx
+ " before the update is "
+ prev);
// Displaying the AtomicIntegerArray
System.out.println("The array after update : "
+ arr);
}
}
输出:
The array : [11, 12, 13, 14, 15] Value at index 0 before the update is 11 The array after update : [100, 12, 13, 14, 15]
示例2:
// Java program that demonstrates
// the getAndSet() function
import java.util.concurrent.atomic.AtomicIntegerArray;
public class GFG {
public static void main(String args[])
{
// Initializing an array
int a[] = { 11, 12, 13, 14, 15 };
// 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;
// New value to set at idx
int val = 10;
// Updating the value at
// idx applying getAndSet
// and store previous value
int prev = arr.getAndSet(idx, val);
// Previous value at idx before update
System.out.println("Value at index " + idx
+ " before the update is "
+ prev);
// Displaying the AtomicIntegerArray
System.out.println("The array after update : "
+ arr);
}
}
输出:
The array : [11, 12, 13, 14, 15] Value at index 3 before the update is 14 The array after update : [11, 12, 13, 10, 15]
相关用法
- Java AtomicBoolean getAndSet()用法及代码示例
- Java AtomicReferenceArray getAndSet()用法及代码示例
- Java AtomicLongArray getAndSet()用法及代码示例
- Java AtomicReference getAndSet()用法及代码示例
- Java AtomicLong getAndSet()用法及代码示例
- Java AtomicInteger getAndSet()用法及代码示例
- Java AtomicIntegerArray get()用法及代码示例
- Java AtomicIntegerArray set()用法及代码示例
- Java AtomicIntegerArray compareAndSet()用法及代码示例
- Java AtomicIntegerArray accumulateAndGet()用法及代码示例
- Java AtomicIntegerArray addAndGet()用法及代码示例
- Java AtomicIntegerArray incrementAndGet()用法及代码示例
- Java AtomicIntegerArray length()用法及代码示例
- Java AtomicIntegerArray getAndIncrement()用法及代码示例
- Java AtomicIntegerArray decrementAndGet()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 AtomicIntegerArray getAndSet() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。