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

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

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

Java.util.concurrent.atomic.AtomicIntegerArray.getAndUpdate()是Java中的一个内置方法,它在对 AtomicIntegerArray 的任何给定索引处的值应用给定的更新函数后更新该索引处的值。该方法将 AtomicIntegerArray 的索引值和更新函数作为参数,并通过对该值应用更新函数来更新该索引处的值。该函数应该没有副作用,因为当尝试更新由于线程之间的争用而失败时,它可能会被重新应用。 getAndUpdate()updateAndGet()函数的唯一区别是前者返回更新前的值,后者返回更新后的值。

句法:


参数:
该函数接受两个参数:

  • i这是要进行更新的索引。
  • updateFunction是单个参数的更新函数,它告诉要进行什么更新。

返回值:该函数返回一个整数值,该值是应用指定更新函数后的值。

下面的程序说明了上述方法:
方案一:

// Java program that demonstrates
// the getAndUpdate() function
  
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.function.IntUnaryOperator;
  
public class GFG {
    public static void main(String args[])
    {
        // Initializing an array
        int a[] = { 1, 2, 3, 4, 5 };
  
        // Initializing an AtomicIntegerArray with array a
        AtomicIntegerArray arr = new AtomicIntegerArray(a);
  
        // Displaying the AtomicIntegerArray
        System.out.println("The array : " + arr);
  
        // Index where update is to be made
        int idx = 3;
  
        // Declaring the updateFunction
        IntUnaryOperator squaredFunction = (l) -> l * l;
  
        // Updating the value at idx
        // applying updateFunction and
        // storing the previous value
        int prev = arr.getAndUpdate(idx, squaredFunction);
  
        // Displaying the previous value
        System.out.println("The value before update"
                           + " at index " + idx
                           + " is " + prev);
  
        // Displaying the AtomicIntegerArray
        System.out.println("The array after update : "
                           + arr);
    }
}
输出:
The array : [1, 2, 3, 4, 5]
The value before update at index 3 is 4
The array after update : [1, 2, 3, 16, 5]

方案二:

// Java program that demonstrates
// the getAndUpdate() function
  
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.function.IntUnaryOperator;
  
public class GFG {
    public static void main(String args[])
    {
        // Initializing an array
        int a[] = { 1, 2, 3, 4, 5 };
  
        // Initializing an AtomicIntegerArray with array a
        AtomicIntegerArray arr = new AtomicIntegerArray(a);
  
        // Displaying the AtomicIntegerArray
        System.out.println("The array : " + arr);
  
        // Index where update is to be made
        int idx = 3;
  
        // Declaring the updateFunction
        IntUnaryOperator cubeFunction = (l) -> l * l * l;
  
        // Updating the value at idx
        // applying updateFunction and
        // storing the previous value
        int prev = arr.getAndUpdate(idx, cubeFunction);
  
        // Displaying the previous value
        System.out.println("The value before update"
                           + " at index " + idx
                           + " is " + prev);
  
        // Displaying the AtomicIntegerArray
        System.out.println("The array after update : "
                           + arr);
    }
}
输出:
The array : [1, 2, 3, 4, 5]
The value before update at index 3 is 4
The array after update : [1, 2, 3, 64, 5]

参考: https: Java/util/concurrent/atomic/AtomicIntegerArray.html#getAndUpdate-int-java.util。函数.IntUnaryOperator-