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


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


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

用法:

public final int getAndUpdate(int i, IntUnaryOperator updateFunction)




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

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

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

以下示例程序旨在说明上述方法:
示例1:

// 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]

示例2:

// 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://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html#getAndUpdate-int-java.util.function.IntUnaryOperator-



相关用法


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