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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。