AtomicReference類的lazySet()方法用於設置此AtomicReference對象的值,該對象具有VarHandle.setRelease(java.lang.Object…)指定的內存效果,以確保在此訪問後不會對先前的加載和存儲進行重新排序。
用法:
public final void lazySet(V newValue)
參數:此方法接受newValue,它是要設置的新值。
返回值:此方法不返回任何內容。
以下示例程序旨在說明lazySet()方法:
程序1:
// Java program to demonstrate
// AtomicReference.lazySet() method
import java.util.concurrent.atomic.AtomicReference;
public class GFG {
public static void main(String[] args)
{
// create an atomic reference object.
AtomicReference<Integer> ref
= new AtomicReference<Integer>();
// set some value using lazySet method
ref.lazySet(67545678);
// print value
System.out.println("Integer value = "
+ ref.get());
}
}
輸出:
Integer value = 67545678
程序2:
// Java program to demonstrate
// AtomicReference.lazySet() method
import java.util.concurrent.atomic.AtomicReference;
public class GFG {
public static void main(String[] args)
{
// create an atomic reference object
AtomicReference<String> ref
= new AtomicReference<String>();
// set some value using lazySet()
ref.lazySet("GFG(GeeksForGeeks)");
// print value
System.out.println(ref.get());
}
}
輸出:
GFG(GeeksForGeeks)
相關用法
- Java AtomicInteger lazySet()用法及代碼示例
- Java AtomicIntegerArray lazySet()用法及代碼示例
- Java AtomicLong lazySet()用法及代碼示例
- Java AtomicLongArray lazySet()用法及代碼示例
- Java AtomicReferenceArray lazySet()用法及代碼示例
- Java AtomicBoolean lazySet()用法及代碼示例
- Java AtomicReference set()用法及代碼示例
- Java AtomicReference get()用法及代碼示例
- Java AtomicReference getAcquire()用法及代碼示例
- Java AtomicReference weakCompareAndSet()用法及代碼示例
- Java AtomicReference setRelease()用法及代碼示例
- Java AtomicReference toString()用法及代碼示例
- Java AtomicReference weakCompareAndSetVolatile()用法及代碼示例
- Java AtomicReference weakCompareAndSetRelease()用法及代碼示例
- Java AtomicReference weakCompareAndSetPlain()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 AtomicReference lazySet() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。