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


Java AtomicLongArray getAndDecrement()用法及代碼示例


Java.util.concurrent.atomic.AtomicLongArray.getAndDecrement()是Java中的一種內置方法,該方法原子地將給定索引處的值減一。此方法獲取AtomicLongArray的索引值,並返回該索引處的值,然後遞減該索引處的值。函數getAndDecrement()與decrementAndGet()類似,但後一個函數在減量後返回值,而前一個函數在減量前返回值。

用法:

public final long getAndDecrement(int i)



參數:該函數接受單個參數i,該參數是執行一個操作減1的索引。

返回值:該函數將在長索引處進行遞減操作之前的值。

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

// Java program that demonstrates 
// the getAndDecrement() function 
  
import java.util.concurrent.atomic.AtomicLongArray; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        // Initializing an array 
        long a[] = { 1, 2, 3, 4, 5 }; 
  
        // Initializing an AtomicLongArray with array a 
        AtomicLongArray arr = new AtomicLongArray(a); 
  
        // Displaying the AtomicLongArray 
        System.out.println("The array : " + arr); 
  
        // Index where operation is performed 
        int idx = 3; 
  
        // Decrementing the value at 
        // idx applying getAndDecrement 
        // and storing previous value 
        long prev = arr.getAndDecrement(idx); 
  
        // The previous value at idx 
        System.out.println("Value at index " + idx 
                           + " before decrement is "
                           + prev); 
  
        // Displaying the AtomicLongArray 
        System.out.println("The array after decrement : "
                           + arr); 
    } 
}
輸出:
The array : [1, 2, 3, 4, 5]
Value at index 3 before decrement is 4
The array after decrement : [1, 2, 3, 3, 5]

示例2:

// Java program that demonstrates 
// the getAndDecrement() function 
  
import java.util.concurrent.atomic.AtomicLongArray; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        // Initializing an array 
        long a[] = { 10, 20, 30, 40, 50 }; 
  
        // Initializing an AtomicLongArray with array a 
        AtomicLongArray arr = new AtomicLongArray(a); 
  
        // Displaying the AtomicLongArray 
        System.out.println("The array : " + arr); 
  
        // Index where operation is performed 
        int idx = 0; 
  
        // Decrementing the value at 
        // idx applying getAndDecrement 
        // and storing previous value 
        long prev = arr.getAndDecrement(idx); 
  
        // The previous value at idx 
        System.out.println("Value at index " + idx 
                           + " before decrement is "
                           + prev); 
  
        // Displaying the AtomicLongArray 
        System.out.println("The array after decrement : "
                           + arr); 
    } 
}
輸出:
The array : [10, 20, 30, 40, 50]
Value at index 0 before decrement is 10
The array after decrement : [9, 20, 30, 40, 50]

參考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicLongArray.html#getAndDecrement-int-



相關用法


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