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

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

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

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

public final E getAndAccumulate(E x,
     BinaryOperator accumulatorFunction)

参数:此方法接受:

  • x是更新后的值,并且
  • accumulatorFunction是两个参数的无副作用函数。

返回值:该方法返回前一个值
下面的程序说明了 getAndAccumulate() 方法:
方案一:

Java
// Java program to demonstrate
// AtomicReference.getAndAccumulate() method
 
import java.util.concurrent.atomic.*;
import java.util.function.BinaryOperator;
 
public class GFG {
    public static void main(String args[])
    {
 
        // AtomicReference with value
        AtomicReference ref
            = new AtomicReference<>(3456);
 
        // Value to apply getAndAccumulate
        int x = 45654;
 
        // Declaring the accumulatorFunction
        // applying function to add value as string
        BinaryOperator add
            = (u, v) -> u.toString() + v.toString();
 
        // apply getAndAccumulate()
        int value = ref.getAndAccumulate(x, add);
 
        // print AtomicReference
        System.out.println(
            "The AtomicReference previous value: "
            + value);
        System.out.println(
            "The AtomicReference new value: "
            + ref.get());
    }
}


Java
// Java program to demonstrate
// AtomicReference.getAndAccumulate() method
 
import java.util.concurrent.atomic.*;
import java.util.function.BinaryOperator;
 
public class GFG {
    public static void main(String args[])
    {
        // AtomicReference with value
        AtomicReference ref
            = new AtomicReference("GFG ");
 
        // Value to apply getAndAccumulate
        String x = "Welcome";
 
        // Declaring the accumulatorFunction
        // applying function to add value as string
        BinaryOperator add
            = (u, v) -> v + " to " + u;
 
        // apply getAndAccumulate()
        String previousValue
            = ref.getAndAccumulate(x, add);
 
        // print AtomicReference
        System.out.println(
            "The AtomicReference previous value: "
            + previousValue);
    }
}


输出:

方案二:

Java

// Java program to demonstrate
// AtomicReference.getAndAccumulate() method
 
import java.util.concurrent.atomic.*;
import java.util.function.BinaryOperator;
 
public class GFG {
    public static void main(String args[])
    {
        // AtomicReference with value
        AtomicReference ref
            = new AtomicReference("GFG ");
 
        // Value to apply getAndAccumulate
        String x = "Welcome";
 
        // Declaring the accumulatorFunction
        // applying function to add value as string
        BinaryOperator add
            = (u, v) -> v + " to " + u;
 
        // apply getAndAccumulate()
        String previousValue
            = ref.getAndAccumulate(x, add);
 
        // print AtomicReference
        System.out.println(
            "The AtomicReference previous value: "
            + previousValue);
    }
}
输出:

参考资料: https: Java Java )