Java AtomicIntegerArray getAndSet()方法及示例

Java AtomicIntegerArray getAndSet()方法及示例

Java.util.concurrent.atomic.AtomicIntegerArray.getAndSet() 是Java中的一个内置方法,它可以在AtomicIntegerArray的任何指定位置原子化地设置一个指定的值。该方法将AtomicIntegerArray的索引值和要设置的值作为参数。在设置该索引的新值之前,它会返回给定索引的值。 getAndSet() 函数与 set() 函数类似,但前者返回值,而后者不返回任何值。

语法

public final int getAndSet(int i, int newValue)

参数: 该函数接受两个参数。

  • i – 要进行更新的索引。
  • newValue – 要在索引i处设置的值

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

下面的程序说明了上述方法。

程序1 :

// Java program that demonstrates
// the getAndSet() 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;
  
        // New value to set at idx
        int val = 100;
  
        // Updating the value at
        // idx applying getAndSet
        // and store previous value
        int prev = arr.getAndSet(idx, val);
  
        // 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 : [100, 12, 13, 14, 15]

程序2

// Java program that demonstrates
// the getAndSet() 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;
  
        // New value to set at idx
        int val = 10;
  
        // Updating the value at
        // idx applying getAndSet
        // and store previous value
        int prev = arr.getAndSet(idx, val);
  
        // 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, 10, 15]

参考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html#getAndSet(int, %20int)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程