當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。