📜  Java中的 AtomicLongArraylazySet() 方法及示例

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

Java中的 AtomicLongArraylazySet() 方法及示例

Java.util.concurrent.atomic.AtomicLongArray.lazySet()是Java中的一个内置方法,它最终在 AtomicLongArray 的任何给定索引处设置给定值。该方法将 AtomicLongArray 的索引值和要设置的值作为参数并更新先前的值而不返回任何内容。

句法:

参数:该函数有两个参数:

  • i这是要进行更新的索引值。
  • newValue是要在索引处更新的新值。

    返回值:该函数不返回任何值。

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

    方案一:

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

    方案二:

    // Java program that demonstrates
    // the lazySet() 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;
      
            // The new value to update at idx
            long val = 100;
      
            // Updating the value at
            // idx applying lazySet
            arr.lazySet(idx, val);
      
            // 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, 100, 5]
    

    参考: https: Java/util/concurrent/atomic/AtomicLongArray.html#lazySet-int-long-