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


Java AtomicIntegerArray getAndDecrement()用法及代码示例


Java.util.concurrent.atomic.AtomicIntegerArray.getAndDecrement()是Java中的一种内置方法,可将给定索引的值原子递减1。此方法获取AtomicIntegerArray的索引值,并返回该索引处的值,然后递减该索引处的值。函数getAndDecrement()与decrementAndGet()类似,但后一个函数在减量后返回值,而前一个函数在减量前返回值。

用法:

public final int getAndDecrement(int i)

参数:该函数接受单个参数i,该参数是执行一个操作减1的索引。


返回值:该函数在以int为索引的减量运算之前返回值。

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

// Java program that demonstrates 
// the getAndDecrement() function 
  
import java.util.concurrent.atomic.AtomicIntegerArray; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        // Initializing an array 
        int a[] = { 1, 2, 3, 4, 5 }; 
  
        // 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; 
  
        // Decrementing the value at 
        // idx applying getAndDecrement 
        // and storing previous value 
        int prev = arr.getAndDecrement(idx); 
  
        // The previous value at idx 
        System.out.println("Value at index " + idx 
                           + " before decrement is "
                           + prev); 
  
        // Displaying the AtomicIntegerArray 
        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.AtomicIntegerArray; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        // Initializing an array 
        int a[] = { 10, 20, 30, 40, 50 }; 
  
        // 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; 
  
        // Decrementing the value at 
        // idx applying getAndDecrement 
        // and storing previous value 
        int prev = arr.getAndDecrement(idx); 
  
        // The previous value at idx 
        System.out.println("Value at index " + idx 
                           + " before decrement is "
                           + prev); 
  
        // Displaying the AtomicIntegerArray 
        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/7/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html#getAndDecrement(int)



相关用法


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