Java中的 AtomicReference setRelease() 方法及示例
AtomicReference类的setRelease()方法用于设置此 AtomicReference 对象的值,具有由 VarHandle.setRelease(Java.lang.Object…) 指定的记忆效果。此方法具有与 memory_order_release 排序兼容的内存排序效果。
句法:
public final void setRelease(V newValue)
参数:此方法接受 newValue,这是要设置的新值。
返回值:此方法不返回任何内容。
下面的程序说明了 setRelease() 方法:
方案一:
// Java program to demonstrate
// AtomicReference.setRelease() method
import java.util.concurrent.atomic.AtomicReference;
public class GFG {
public static void main(String[] args)
{
// create an atomic reference object.
AtomicReference ref
= new AtomicReference();
// set some value using setRelease method
ref.setRelease(123457876L);
// print value
System.out.println("value = " + ref.get());
}
}
输出:
方案二:
// Java program to demonstrate
// AtomicReference.setRelease() method
import java.util.concurrent.atomic.AtomicReference;
public class GFG {
public static void main(String[] args)
{
// create an atomic reference object
AtomicReference ref
= new AtomicReference();
// set some value using setRelease()
ref.setRelease("String Builder");
// print value
System.out.println("Class = " + ref.get());
}
}
输出:
参考资料: https: Java/util/concurrent/atomic/AtomicReference.html#setRelease(V)