當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。