Java中的 AtomicReference setPlain() 方法及示例
AtomicReference类的setPlain()方法用于设置此 AtomicReference 对象的值,其内存语义设置就像变量被声明为非易失性和非最终的一样。
句法:
public final void setPlain(V newValue)
参数:此方法接受 newValue,这是要设置的新值。
返回值:此方法不返回任何内容。
下面的程序说明了 setPlain() 方法:
方案一:
// Java program to demonstrate
// AtomicReference.setPlain() 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 setPlain method
ref.setPlain(999945678979L);
// print value
System.out.println("value = " + ref.get());
}
}
输出:
方案二:
// Java program to demonstrate
// AtomicReference.setPlain() 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 setPlain()
ref.setPlain("String Buffer");
// print value
System.out.println("Class = " + ref.get());
}
}
输出:
参考资料: https: Java/util/concurrent/atomic/AtomicReference.html#setPlain(V)