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


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


AtomicReferenceArray类的getAndAccumulate()方法用于原子更新AtomicReferenceArray索引i处的元素,并将给定的accumulatorFunction应用于当前值和给定值,并返回前一个值。 accumulatorFunction应该没有副作用,因为当尝试更新由于线程之间的争用而失败时,可能会重新应用accumulatorFunction。应用该函数时,将索引i处的当前值作为其第一个参数,并将给定的update作为第二个参数。

用法:

public final E getAndAccumulate(int i, E x,
     BinaryOperator<E> accumulatorFunction)

参数:此方法接受:


  • i这是执行操作所接受的AtomicReferenceArray的索引,
  • x这是更新的值,
  • accumulatorFunction这是两个参数的无副作用函数。

返回值:此方法返回前一个值。

以下示例程序旨在说明getAndAccumulate()方法:
程序1:

// Java program to demonstrate 
// getAndAccumulate() 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:"
            + array); 
  
        // Index and Value to apply getAndAccumulate 
        int index = 2; 
        int E = 3; 
  
        // Declaring the accumulatorFunction 
        // applying function 
        BinaryOperator add 
            = (u, v) 
            -> Integer.parseInt( 
                   u.toString()) 
                   * Integer 
                         .parseInt( 
                             v.toString()); 
  
        // apply getAndAccumulate() 
        int value 
            = array.getAndAccumulate(index, E, add); 
  
        // print AtomicReferenceArray 
        System.out.println( 
            "previous value of index 2:"
            + value); 
        System.out.println( 
            "The AtomicReferenceArray after update:"
            + array); 
    } 
}
输出:

程序2:

// Java program to demonstrate 
// getAndAccumulate() 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> array 
            = new AtomicReferenceArray<>(a); 
  
        // Print AtomicReferenceArray 
        System.out.println( 
            "The AtomicReferenceArray before update:"
            + array); 
  
        // Index and Value to apply getAndAccumulate 
        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 getAndAccumulate() 
        String value 
            = array.getAndAccumulate(index, E, add); 
  
        // print AtomicReferenceArray 
        System.out.println( 
            "previous value of index 1:"
            + value); 
        System.out.println( 
            "The AtomicReferenceArray after update:"
            + array); 
    } 
}
输出:

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



相关用法


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