📌  相关文章
📜  Java中的 AtomicReferenceArray getAndAccumulate() 方法及示例

📅  最后修改于: 2022-05-13 01:55:50.748000             🧑  作者: Mango

Java中的 AtomicReferenceArray getAndAccumulate() 方法及示例

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

句法:

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

参数:此方法接受:

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

返回值:该方法返回前一个值

下面的程序说明了 getAndAccumulate() 方法:
方案一:

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

方案二:

// 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 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: Java Java )