Java AtomicLongArray lazySet()方法及示例

Java AtomicLongArray lazySet()方法及示例

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

语法

public final void lazySet(int i, long newValue)

参数: 该函数需要两个参数。

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

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

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

程序1 :

// 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]

程序2

// 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://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicLongArray.html#lazySet-int-long-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程