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


Java AtomicIntegerArray getAndSet()用法及代碼示例


Java.util.concurrent.atomic.AtomicIntegerArray.getAndSet()是Java中的一種內置方法,該方法原子地在AtomicIntegerArray的任何給定位置設置給定值。該方法采用AtomicIntegerArray的索引值和要設置為參數的值。在給定索引處設置新值之前,它將返回給定索引處的值。函數getAndSet()與set()函數相似,但前者返回值,而後者不返回任何值。

用法:

public final int getAndSet(int i, int newValue)

參數:該函數接受兩個參數:


  • i–要進行更新的索引。
  • newValue–在索引i處設置的值

返回值:函數以整數形式返回更新前給定索引處的值。

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

示例1:

// Java program that demonstrates 
// the getAndSet() function 
  
import java.util.concurrent.atomic.AtomicIntegerArray; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        // Initializing an array 
        int a[] = { 11, 12, 13, 14, 15 }; 
  
        // Initializing an AtomicIntegerArray with array a 
        AtomicIntegerArray arr = new AtomicIntegerArray(a); 
  
        // Displaying the AtomicIntegerArray 
        System.out.println("The array : " + arr); 
  
        // Index where operation is performed 
        int idx = 0; 
  
        // New value to set at idx 
        int val = 100; 
  
        // Updating the value at 
        // idx applying getAndSet 
        // and store previous value 
        int prev = arr.getAndSet(idx, val); 
  
        // Previous value at idx before update 
        System.out.println("Value at index " + idx 
                           + " before the update is "
                           + prev); 
  
        // Displaying the AtomicIntegerArray 
        System.out.println("The array after update : "
                           + arr); 
    } 
}
輸出:
The array : [11, 12, 13, 14, 15]
Value at index 0 before the update is 11
The array after update : [100, 12, 13, 14, 15]

示例2:

// Java program that demonstrates 
// the getAndSet() function 
  
import java.util.concurrent.atomic.AtomicIntegerArray; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        // Initializing an array 
        int a[] = { 11, 12, 13, 14, 15 }; 
  
        // Initializing an AtomicIntegerArray with array a 
        AtomicIntegerArray arr = new AtomicIntegerArray(a); 
  
        // Displaying the AtomicIntegerArray 
        System.out.println("The array : " + arr); 
  
        // Index where operation is performed 
        int idx = 3; 
  
        // New value to set at idx 
        int val = 10; 
  
        // Updating the value at 
        // idx applying getAndSet 
        // and store previous value 
        int prev = arr.getAndSet(idx, val); 
  
        // Previous value at idx before update 
        System.out.println("Value at index " + idx 
                           + " before the update is "
                           + prev); 
  
        // Displaying the AtomicIntegerArray 
        System.out.println("The array after update : "
                           + arr); 
    } 
}
輸出:
The array : [11, 12, 13, 14, 15]
Value at index 3 before the update is 14
The array after update : [11, 12, 13, 10, 15]

參考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html#getAndSet(int, %20int)



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 AtomicIntegerArray getAndSet() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。