📌  相关文章
📜  Java中的 AtomicLongArray getAndSet() 方法及示例

📅  最后修改于: 2022-05-13 01:55:09.661000             🧑  作者: Mango

Java中的 AtomicLongArray getAndSet() 方法及示例

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

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

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

    返回值:该函数返回更新前给定索引处的值,该值位于long中。

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

    方案一:

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

    方案二:

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

    参考:
    https://docs.oracle.com/javase/8/docs/api/java Java