AtomicReferenceArray類的getAndAccumulate()方法用於原子更新AtomicReferenceArray索引i處的元素,並將給定的accumulatorFunction應用於當前值和給定值,並返回前一個值。 accumulatorFunction應該沒有副作用,因為當嘗試更新由於線程之間的爭用而失敗時,可能會重新應用accumulatorFunction。應用該函數時,將索引i處的當前值作為其第一個參數,並將給定的update作為第二個參數。
用法:
public final E getAndAccumulate(int i, E x, BinaryOperator<E> accumulatorFunction)
參數:此方法接受:
- i這是執行操作所接受的AtomicReferenceArray的索引,
- x這是更新的值,
- accumulatorFunction這是兩個參數的無副作用函數。
返回值:此方法返回前一個值。
以下示例程序旨在說明getAndAccumulate()方法:
程序1:
// Java program to demonstrate
// getAndAccumulate() method
import java.util.concurrent.atomic.*;
import java.util.function.BinaryOperator;
public class GFG {
public static void main(String args[])
{
// an array
Integer a[]
= { 123, 1232, 1433, 134, 13415, 1343 };
// AtomicReferenceArray with array
AtomicReferenceArray<Integer> array
= new AtomicReferenceArray<>(a);
// Print AtomicReferenceArray
System.out.println(
"The AtomicReferenceArray before update:"
+ array);
// Index and Value to apply getAndAccumulate
int index = 2;
int E = 3;
// Declaring the accumulatorFunction
// applying function
BinaryOperator add
= (u, v)
-> Integer.parseInt(
u.toString())
* Integer
.parseInt(
v.toString());
// apply getAndAccumulate()
int value
= array.getAndAccumulate(index, E, add);
// print AtomicReferenceArray
System.out.println(
"previous value of index 2:"
+ value);
System.out.println(
"The AtomicReferenceArray after update:"
+ array);
}
}
輸出:
程序2:
// Java program to demonstrate
// getAndAccumulate() method
import java.util.concurrent.atomic.*;
import java.util.function.BinaryOperator;
public class GFG {
public static void main(String args[])
{
// an array
String a[] = { "GFG", "JS" };
// AtomicReferenceArray with array
AtomicReferenceArray<String> array
= new AtomicReferenceArray<>(a);
// Print AtomicReferenceArray
System.out.println(
"The AtomicReferenceArray before update:"
+ array);
// Index and Value to apply getAndAccumulate
int index = 1;
String E = " PYTHON";
// Declaring the accumulatorFunction
// applying function to add value as string
BinaryOperator add
= (u, v) -> u.toString() + " and " + v.toString();
// apply getAndAccumulate()
String value
= array.getAndAccumulate(index, E, add);
// print AtomicReferenceArray
System.out.println(
"previous value of index 1:"
+ value);
System.out.println(
"The AtomicReferenceArray after update:"
+ array);
}
}
輸出:
相關用法
- Java AtomicInteger getAndAccumulate()用法及代碼示例
- Java AtomicLongArray getAndAccumulate()用法及代碼示例
- Java AtomicReference getAndAccumulate()用法及代碼示例
- Java AtomicReferenceArray set()用法及代碼示例
- Java AtomicReferenceArray get()用法及代碼示例
- Java AtomicReferenceArray compareAndExchange()用法及代碼示例
- Java AtomicReferenceArray compareAndExchangeAcquire()用法及代碼示例
- Java AtomicReferenceArray getOpaque()用法及代碼示例
- Java AtomicReferenceArray getPlain()用法及代碼示例
- Java AtomicReferenceArray lazySet()用法及代碼示例
- Java AtomicReferenceArray getAndSet()用法及代碼示例
- Java AtomicReferenceArray compareAndExchangeRelease()用法及代碼示例
- Java AtomicReferenceArray compareAndSet()用法及代碼示例
- Java AtomicReferenceArray weakCompareAndSetPlain()用法及代碼示例
- Java AtomicReferenceArray weakCompareAndSetVolatile()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 AtomicReferenceArray getAndAccumulate() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。