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


Java AtomicLongArray decrementAndGet()用法及代码示例


Java.util.concurrent.atomic.AtomicLongArray.decrementAndGet()是Java中的一种内置方法,它在给定索引上原子减少一个元素。此方法将索引值和要添加的值作为参数,并在该索引处返回更新后的值。

用法:

public final long decrementAndGet(int i)

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


返回值:函数返回更新后的值,该值为long类型。

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

// Java program that demonstrates 
// the compareAndSet() 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; 
  
        // Updating the value at 
        // idx applying decrementAndGet 
        arr.decrementAndGet(idx); 
  
        // Displaying the AtomicLongArray 
        System.out.println("The array after update : "
                           + arr); 
    } 
}
输出:
The array : [1, 2, 3, 4, 5]
The array after update : [1, 2, 3, 3, 5]

示例2:

// Java program that demonstrates 
// the compareAndSet() 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 = 0; 
  
        // Updating the value at 
        // idx applying decrementAndGet 
        arr.decrementAndGet(idx); 
  
        // Displaying the AtomicLongArray 
        System.out.println("The array after update : "
                           + arr); 
    } 
}
输出:
The array : [1, 2, 3, 4, 5]
The array after update : [0, 2, 3, 4, 5]

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



相关用法


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