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

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

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

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

public final V updateAndGet(
  UnaryOperator updateFunction)

参数:此方法接受:

  • 索引i更新索引值和
  • updateFunction是一个无副作用的函数。

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

Java
// Java program to demonstrate
// AtomicReferenceArray.updateAndGet() method
 
import java.util.concurrent.atomic.*;
import java.util.function.UnaryOperator;
 
public class GFG {
    public static void main(String args[])
    {
        // an array
        String a[]
            = { "GFG", "JS", "PYTHON", "JAVA" };
 
        // AtomicReferenceArray with array
        AtomicReferenceArray array
            = new AtomicReferenceArray<>(a);
 
        // Print AtomicReferenceArray
        System.out.println(
            "The AtomicReferenceArray before update : "
            + array);
 
        // Index
        int index = 2;
 
        // Declaring the accumulatorFunction
        // applying function
        UnaryOperator add
            = (u) -> u.toString() + " and ML";
 
        // apply updateAndGet()
        String value
            = array.updateAndGet(index, add);
 
        // print AtomicReferenceArray
        System.out.println("updated value of index 2:"
                           + value);
        System.out.println(
            "The AtomicReferenceArray after update: "
            + array);
    }
}


Java
// Java program to demonstrate
// AtomicReferenceArray.updateAndGet() method
 
import java.util.concurrent.atomic.*;
import java.util.function.UnaryOperator;
 
public class GFG {
    public static void main(String args[])
    {
        // an array
        Integer a[]
            = { 213, 1234, 4543, 345 };
 
        // AtomicReferenceArray with array
        AtomicReferenceArray array
            = new AtomicReferenceArray(a);
 
        // Print AtomicReferenceArray
        System.out.println(
            "The AtomicReferenceArray before update : "
            + array);
 
        // Index
        int index = 2;
 
        // Declaring the accumulatorFunction
        // applying function
        UnaryOperator add
            = (u) -> Integer.parseInt(u.toString()) * 500;
 
        // apply updateAndGet()
        int value
            = array.updateAndGet(index, add);
 
        // print AtomicReferenceArray
        System.out.println("updated value of index 2:"
                           + value);
        System.out.println(
            "The AtomicReferenceArray after update: "
            + array);
    }
}


输出:

方案二:

Java

// Java program to demonstrate
// AtomicReferenceArray.updateAndGet() method
 
import java.util.concurrent.atomic.*;
import java.util.function.UnaryOperator;
 
public class GFG {
    public static void main(String args[])
    {
        // an array
        Integer a[]
            = { 213, 1234, 4543, 345 };
 
        // AtomicReferenceArray with array
        AtomicReferenceArray array
            = new AtomicReferenceArray(a);
 
        // Print AtomicReferenceArray
        System.out.println(
            "The AtomicReferenceArray before update : "
            + array);
 
        // Index
        int index = 2;
 
        // Declaring the accumulatorFunction
        // applying function
        UnaryOperator add
            = (u) -> Integer.parseInt(u.toString()) * 500;
 
        // apply updateAndGet()
        int value
            = array.updateAndGet(index, add);
 
        // print AtomicReferenceArray
        System.out.println("updated value of index 2:"
                           + value);
        System.out.println(
            "The AtomicReferenceArray after update: "
            + array);
    }
}
输出:

参考: https: Java Java 函数)