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


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


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); 
    } 
}
输出:

参考文献: https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReferenceArray.html#accumulateAndGet(int, E, java.util.function.BinaryOperator)



相关用法


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