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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。