当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java AtomicLongArray getAndUpdate()用法及代码示例


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

用法:

public final long getAndUpdate(int i, LongUnaryOperator updateFunction)




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

  • i这是要进行更新的索引。
  • updateFunction这是单个参数的更新函数,用于指示要进行的更新。

返回值:该函数返回一个long值,该值是应用指定的update函数后的值。

以下示例程序旨在说明上述方法:
示例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-



相关用法


注:本文由纯净天空筛选整理自rupesh_rao大神的英文原创作品 AtomicLongArray getAndUpdate() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。