Java AtomicLongArray getAndUpdate()方法及示例

Java AtomicLongArray getAndUpdate()方法及示例

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

语法

public final long getAndUpdate(int i, LongUnaryOperator updateFunction)

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

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

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

以下程序说明了上述方法:

程序1 :

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

程序2

// Java program that demonstrates
// the getAndUpdate() function
  
import java.util.concurrent.atomic.AtomicLongArray;
import java.util.function.LongUnaryOperator;
  
public class GFG {
    public static void main(String args[])
    {
        // Initializing an array
        long a[] = { 1, 2, 3, 4, 5 };
  
        // Initializing an AtomicLongArray with array a
        AtomicLongArray arr = new AtomicLongArray(a);
  
        // Displaying the AtomicLongArray
        System.out.println("The array : " + arr);
  
        // Index where update is to be made
        int idx = 3;
  
        // Declaring the updateFunction
        LongUnaryOperator cubeFunction = (l) -> l * l * l;
  
        // Updating the value at idx
        // applying updateFunction and
        // storing the previous value
        long prev = arr.getAndUpdate(idx, cubeFunction);
  
        // Displaying the previous value
        System.out.println("The value before update"
                           + " at index " + idx
                           + " is " + prev);
  
        // Displaying the AtomicLongArray
        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://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicLongArray.html#getAndUpdate-int-java.util.function.LongUnaryOperator-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程