Java中的 AtomicReference getPlain() 方法及示例
AtomicReference类的getPlain()方法用于返回 AtomicReference 对象的当前值,具有读取的内存语义,就好像变量被声明为非易失性一样。
句法:
public final V getPlain()
参数:此方法不接受任何内容。
返回值:该方法返回 AtomicReference 的值。
下面的程序说明了 getPlain() 方法:
方案一:
// Java program to demonstrate
// AtomicReference.getPlain() 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
ref.set(564324);
// get value using getPlain()
int value = ref.getPlain();
// print
System.out.println("Value = " + value);
}
}
输出:
方案二:
// Java program to demonstrate
// AtomicReference.getPlain() 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
ref.set("AtomicReference value");
// get value getPlain()
String value = ref.getPlain();
// print
System.out.println("Value = " + value);
}
}
输出:
参考资料: https: Java/util/concurrent/atomic/AtomicReference.html#getPlain(V)