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


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


Java.util.concurrent.atomic.AtomicLongArray.getAndIncrement()是Java中的一种内置方法,可以自动将AtomicLongArray的任何索引上存在的值加1。该方法将AtomicLongArray的索引值作为参数,在递增之前返回该索引处的值,然后在该索引处递增该值。函数getAndIncrement()与incrementAndGet()函数相似,但前者返回增量前的值,而后者返回增量操作后的值。

用法:

public final long getAndIncrement(int i)

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


返回值:函数将在很长的更新之前返回该索引处的先前值。

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

示例1:

// Java program that demonstrates 
// the getAndIncrement() function 
  
import java.util.concurrent.atomic.AtomicLongArray; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        // Initializing an array 
        long a[] = { 11, 12, 13, 14, 15 }; 
  
        // 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 getAndIncrement 
        // and storing the previous value 
        long prev = arr.getAndIncrement(idx); 
  
        // Previous value at idx before update 
        System.out.println("Value at index " + idx 
                           + " before the update is "
                           + prev); 
  
        // Displaying the AtomicLongArray 
        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 : [12, 12, 13, 14, 15]

示例2:

// Java program that demonstrates 
// the getAndIncrement() function 
  
import java.util.concurrent.atomic.AtomicLongArray; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        // Initializing an array 
        long a[] = { 11, 12, 13, 14, 15 }; 
  
        // 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 getAndIncrement 
        // and storing the previous value 
        long prev = arr.getAndIncrement(idx); 
  
        // Previous value at idx before update 
        System.out.println("Value at index " + idx 
                           + " before the update is "
                           + prev); 
  
        // Displaying the AtomicLongArray 
        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, 15, 15]

注意:函数getAndIncrement()与incrementAndGet()类似,但后一个函数返回更新后的值,而前一个函数在更新后返回前一个值。

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



相关用法


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