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


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


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

用法:

public final E 
  getAndUpdate(int i, 
    UnaryOperator<E> updateFunction)

参数:此方法接受:


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

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

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

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

程序2:

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

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



相关用法


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