Java.util.concurrent.atomic.AtomicLongArray.getAndAccumulate()是Java中的一种内置方法,在将给定函数应用于当前值和给定值后,原子地更新AtomicLongArray的任何索引的值,并返回先前的值。此方法接受要执行操作的AtomicLongArray的索引,执行操作所用的值以及累加器函数作为参数。应用该函数时,将index处的当前值作为其第一个参数,并将给定的update作为第二个参数。累加器函数应无副作用,因为当尝试更新由于线程间争用而失败时,可以重新应用累加器函数。函数getAndAccumulate()与accumulateAndGet()类似,但前一个函数在更新前返回值,而后者在更新后返回值。
用法:
public final long getAndAccumulate(int i, long x, LongBinaryOperator accumulatorFunction)
参数:该函数接受三个参数:
- i–要进行更新的索引。
- x–以i处的值进行运算的值
- accumulatorFunction–两个参数的无副作用函数。
返回值:该函数返回long类型的更新之前的值。
以下示例程序旨在说明上述方法:
示例1:
// Java program that demonstrates
// the getAndAccumulate() function
import java.util.concurrent.atomic.AtomicLongArray;
import java.util.function.LongBinaryOperator;
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 where update is to be made
int idx = 4;
// Value to make operation with value at idx
long x = 5;
// Declaring the accumulatorFunction
LongBinaryOperator add = (u, v) -> u + v;
// Updating the value at idx
// applying getAndAccumulate
long prev = arr.getAndAccumulate(idx, x, add);
// The previous value at idx
System.out.println("Value at index " + idx
+ " before update is "
+ prev);
// Displaying the AtomicLongArray
System.out.println("The array after update : "
+ arr);
}
}
输出:
The array : [1, 2, 3, 4, 5] Value at index 4 before update is 5 The array after update : [1, 2, 3, 4, 10]
示例2:
// Java program that demonstrates
// the getAndAccumulate() function
import java.util.concurrent.atomic.AtomicLongArray;
import java.util.function.LongBinaryOperator;
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 where update is to be made
int idx = 0;
// Value to make operation with value at idx
long x = 6;
// Declaring the accumulatorFunction
LongBinaryOperator sub = (u, v) -> u - v;
// Updating the value at idx
// applying getAndAccumulate
long prev = arr.getAndAccumulate(idx, x, sub);
// The previous value at idx
System.out.println("Value at index " + idx
+ " before update is "
+ prev);
// Displaying the AtomicLongArray
System.out.println("The array after update : "
+ arr);
}
}
输出:
The array : [1, 2, 3, 4, 5] Value at index 0 before update is 1 The array after update : [-5, 2, 3, 4, 5]
相关用法
- Java AtomicReferenceArray getAndAccumulate()用法及代码示例
- Java AtomicReference getAndAccumulate()用法及代码示例
- Java AtomicInteger getAndAccumulate()用法及代码示例
- Java AtomicLongArray set()用法及代码示例
- Java AtomicLongArray get()用法及代码示例
- Java AtomicLongArray incrementAndGet()用法及代码示例
- Java AtomicLongArray toString()用法及代码示例
- Java AtomicLongArray updateAndGet()用法及代码示例
- Java AtomicLongArray getAndUpdate()用法及代码示例
- Java AtomicLongArray getAndAdd()用法及代码示例
- Java AtomicLongArray getAndSet()用法及代码示例
- Java AtomicLongArray decrementAndGet()用法及代码示例
- Java AtomicLongArray compareAndSet()用法及代码示例
- Java AtomicLongArray lazySet()用法及代码示例
- Java AtomicLongArray accumulateAndGet()用法及代码示例
注:本文由纯净天空筛选整理自rupesh_rao大神的英文原创作品 AtomicLongArray getAndAccumulate() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。