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

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

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

AtomicReference类的getAndUpdate()方法用于原子更新,通过对当前值应用指定的 updateFunction 操作来更新 AtomicReference 的当前值。它以 updateFunction 接口的一个对象作为其参数,并将对象中指定的操作应用于当前值。它返回之前的值。
句法:

public final V getAndUpdate(UnaryOperator updateFunction)

参数:此方法接受updateFunction这是一个无副作用的函数。
返回值:该方法返回前一个值
下面的程序说明了 getAndUpdate() 方法:
方案一:

Java
// Java program to demonstrate
// AtomicReference.getAndUpdate() method
 
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.UnaryOperator;
 
public class GFG {
    public static void main(String args[])
    {
 
        // AtomicReference with value
        AtomicReference ref
            = new AtomicReference<>(987654);
 
        // Declaring the updateFunction
        // applying function
        UnaryOperator twoDigits
            = (v)
            -> v.toString()
                   .substring(0, 2);
 
        // apply getAndUpdate()
        int value = ref.getAndUpdate(twoDigits);
 
        // 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.getAndUpdate() method
 
import java.util.concurrent.atomic.*;
import java.util.function.UnaryOperator;
 
public class GFG {
    public static void main(String args[])
    {
 
        // AtomicReference with value
        AtomicReference ref
            = new AtomicReference<>("welcome");
 
        // Declaring the updateFunction
        // applying function
        UnaryOperator twoDigits
            = (v) -> v + " to gfg";
 
        // apply getAndUpdate()
        String value
            = ref.getAndUpdate(twoDigits);
 
        // 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.getAndUpdate() method
 
import java.util.concurrent.atomic.*;
import java.util.function.UnaryOperator;
 
public class GFG {
    public static void main(String args[])
    {
 
        // AtomicReference with value
        AtomicReference ref
            = new AtomicReference<>("welcome");
 
        // Declaring the updateFunction
        // applying function
        UnaryOperator twoDigits
            = (v) -> v + " to gfg";
 
        // apply getAndUpdate()
        String value
            = ref.getAndUpdate(twoDigits);
 
        // print AtomicReference
        System.out.println(
            "The AtomicReference previous value: "
            + value);
        System.out.println(
            "The AtomicReference new value: "
            + ref.get());
    }
}
输出:

参考: 函数 : Java Java )