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


Java AtomicReferenceArray updateAndGet()用法及代码示例


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

用法:

public final V updateAndGet(
  UnaryOperator<V> updateFunction)

参数:此方法接受:


  • 索引i以更新索引值,并且
  • updateFunction这是没有副作用的函数。

返回值:此方法返回prevoius值。

以下示例程序旨在说明updateAndGet()方法:
程序1:

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

程序2:

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

参考文献: https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReferenceArray.html#updateAndGet(java.util.function.UnaryOperator)



相关用法


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