AtomicReferenceArray類的accumulateAndGet()方法用於原子更新AtomicReferenceArray索引i處的元素,並將給定的accumulatorFunction應用於當前值和給定值,並返回更新後的值。 accumulatorFunction應該沒有副作用,因為當嘗試更新由於線程之間的爭用而失敗時,可能會重新應用accumulatorFunction。應用該函數時,將索引i處的當前值作為其第一個參數,並將給定的update作為第二個參數。
用法:
public final E accumulateAndGet(int i, E x, BinaryOperator<E> accumulatorFunction)
參數:此方法接受:
- i這是執行操作所接受的AtomicReferenceArray的索引,
- x這是更新的值,
- accumulatorFunction這是兩個參數的無副作用函數。
返回值:此方法返回更新的值。
以下示例程序旨在說明accumulateAndGet()方法:
程序1:
// Java program to demonstrate
// accumulateAndGet() 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\n:"
+ array);
// Index and Value to apply accumulateAndGet
int index = 2;
int E = 343;
// Declaring the accumulatorFunction
// applying function to add value as string
BinaryOperator add
= (u, v)
-> u.toString()
+ v.toString();
// apply accumulateAndGet()
array.accumulateAndGet(index, E, add);
// print AtomicReferenceArray
System.out.println(
"The AtomicReferenceArray "
+ "after update \n:"
+ array);
}
}
輸出:
程序2:
// Java program to demonstrate
// accumulateAndGet() 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> arra
= new AtomicReferenceArray<>(a);
// Print AtomicReferenceArray
System.out.println(
"The AtomicReferenceArray"
+ " before update \n:"
+ array);
// Index and Value to apply accumulateAndGet
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 accumulateAndGet()
array.accumulateAndGet(index, E, add);
// print AtomicReferenceArray
System.out.println(
"The AtomicReferenceArray"
+ " after update \n:"
+ array);
}
}
輸出:
相關用法
- Java AtomicReference accumulateAndGet()用法及代碼示例
- Java AtomicLong accumulateAndGet()用法及代碼示例
- Java AtomicInteger accumulateAndGet()用法及代碼示例
- Java AtomicIntegerArray accumulateAndGet()用法及代碼示例
- Java AtomicLongArray accumulateAndGet()用法及代碼示例
- Java AtomicReferenceArray set()用法及代碼示例
- Java AtomicReferenceArray get()用法及代碼示例
- Java AtomicReferenceArray getAcquire()用法及代碼示例
- Java AtomicReferenceArray compareAndSet()用法及代碼示例
- Java AtomicReferenceArray weakCompareAndSetPlain()用法及代碼示例
- Java AtomicReferenceArray getAndUpdate()用法及代碼示例
- Java AtomicReferenceArray updateAndGet()用法及代碼示例
- Java AtomicReferenceArray getAndAccumulate()用法及代碼示例
- Java AtomicReferenceArray weakCompareAndSetVolatile()用法及代碼示例
- Java AtomicReferenceArray weakCompareAndSetRelease()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 AtomicReferenceArray accumulateAndGet() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。