Java AtomicIntegerArray getAndIncrement()方法及示例

Java AtomicIntegerArray getAndIncrement()方法及示例

Java.util.concurrent.atomic.AtomicIntegerArray.getAndIncrement() 是Java中的一个内置方法,它可以将AtomicIntegerArray的任何索引上的值原子化地增加1。该方法以AtomicIntegerArray的索引值为参数,返回递增前该索引的值,然后递增该索引的值。 getAndIncrement() 函数与 incrementAndGet() 函数类似,但前者返回增量之前的值,而后者则返回增量操作之后的值。

语法

public final int getAndIncrement(int i)

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

返回值: 该函数返回更新前该索引的先前值,该值为整数。

以下程序说明了上述方法。

程序1 :

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程