当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java AtomicLongArray accumulateAndGet()用法及代码示例


Java.util.concurrent.atomic.AtomicLongArray.accumulateAndGet()是Java中的一种内置方法,该方法原子地更新索引i的元素,其结果是将给定函数应用于当前值和给定值,并返回更新后的值。该函数应无副作用,因为当尝试更新由于线程间争用而失败时,可能会重新应用该函数。应用该函数时,将索引i处的当前值作为其第一个参数,并将给定的update作为第二个参数。

用法:

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



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

  • i–将要更新的索引。
  • x–以i处的值进行运算的值
  • accumulatorFunction–两个参数的无副作用函数。

返回值:函数返回的更新值过长。

以下示例程序旨在说明上述方法:
示例1:

// Java program that demonstrates 
// the accumulateAndGet() 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 accumulatorFunction 
        arr.accumulateAndGet(idx, x, add); 
  
        // Displaying the AtomicLongArray 
        System.out.println("The array after update : "
                           + arr); 
    } 
}
输出:
The array : [1, 2, 3, 4, 5]
The array after update : [1, 2, 3, 4, 10]

示例2:

// Java program that demonstrates 
// the accumulateAndGet() 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[] = { 17, 22, 33, 44, 55 }; 
  
        // 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 accumulatorFunction 
        arr.accumulateAndGet(idx, x, sub); 
  
        // Displaying the AtomicLongArray 
        System.out.println("The array after update : "
                           + arr); 
    } 
}
输出:
The array : [17, 22, 33, 44, 55]
The array after update : [11, 22, 33, 44, 55]

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



相关用法


注:本文由纯净天空筛选整理自rupesh_rao大神的英文原创作品 AtomicLongArray accumulateAndGet() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。