当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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-



相关用法


注:本文由纯净天空筛选整理自rupesh_rao大神的英文原创作品 AtomicLongArray lazySet() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。