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

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

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

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

public final E 
  getAndUpdate(int i, 
    UnaryOperator updateFunction)

参数:此方法接受:

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

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

Java
// Java program to demonstrate
// AtomicReferenceArray.getAndUpdate() 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 getAndUpdate()
        String value
            = array.getAndUpdate(index, add);
 
        // print AtomicReferenceArray
        System.out.println("previous value of index 2:"
                           + value);
        System.out.println("The AtomicReferenceArray "
                           + "after update: "
                           + array);
    }
}


Java
// Java program to demonstrate
// AtomicReferenceArray.getAndUpdate() 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()) * 200;
 
        // apply getAndUpdate()
        int value = array.getAndUpdate(index, add);
 
        // print AtomicReferenceArray
        System.out.println("previous value of index 2:"
                           + value);
        System.out.println("updated value of index 2:"
                           + array.get(2));
        System.out.println("The AtomicReferenceArray "
                           + "after update: "
                           + array);
    }
}


输出:

方案二:

Java

// Java program to demonstrate
// AtomicReferenceArray.getAndUpdate() 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()) * 200;
 
        // apply getAndUpdate()
        int value = array.getAndUpdate(index, add);
 
        // print AtomicReferenceArray
        System.out.println("previous value of index 2:"
                           + value);
        System.out.println("updated value of index 2:"
                           + array.get(2));
        System.out.println("The AtomicReferenceArray "
                           + "after update: "
                           + array);
    }
}
输出:

参考: 函数 : Java Java )