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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。