Java AtomicLongArray getAndAccumulate()方法及示例

Java AtomicLongArray getAndAccumulate()方法及示例

Java.util.concurrent.atomic.AtomicLongArray.getAndAccumulate() 是java中的一个内置方法,它可以在AtomicLongArray的任何索引处用给定的函数对当前值和给定值进行原子化更新,并返回之前的值。这个方法接受要进行操作的AtomicLongArray的索引、要进行操作的值和累积器函数作为参数。该函数以索引处的当前值为第一参数,以给定的更新值为第二参数来应用。累加器函数应该是无副作用的,因为当试图更新由于线程之间的争夺而失败时,它可能被重新应用。 getAndAccumulate() 函数与 accumulateAndGet() 函数类似,但前一个函数返回更新前的值,而后者则返回更新后的值。

语法

public final long getAndAccumulate(int i, long x, LongBinaryOperator accumulatorFunction)

参数: 该函数接受三个参数。

  • i – 要进行更新的索引。
  • x – 要对i处的值进行操作的值。
  • accumulatorFunction – 一个有两个参数的无副作用的函数。

返回值: 该函数返回更新前的值,该值为长。

以下程序说明了上述方法:

程序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]

参考资料:
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicLongArray.html#getAndAccumulate-int-long-java.util.function.LongBinaryOperator-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程