Java中的 AtomicReference get() 方法及示例
AtomicReference类的get()方法用于返回此 AtomicReference 对象的值,该对象具有读取的内存语义,就好像该变量被声明为 volatile 类型的变量一样。
句法:
public final V get()
参数:此方法不接受任何内容。
返回值:此方法返回此 AtomicReference 的当前值。
下面的程序说明了 get() 方法:
方案一:
// Java program to demonstrate
// AtomicReference.get() method
import java.util.concurrent.atomic.AtomicReference;
public class GFG {
public static void main(String[] args)
{
// create an atomic reference object
// which stores Integer.
AtomicReference ref
= new AtomicReference();
// set some value
ref.set(12);
// get and print value
int value = ref.get();
System.out.println("value = " + value);
}
}
输出:
value = 12
方案二:
// Java program to demonstrate
// AtomicReference.get() method
import java.util.concurrent.atomic.AtomicReference;
public class GFG {
public static void main(String[] args)
{
// create an atomic reference object
// which stores String.
AtomicReference ref
= new AtomicReference();
// set some value
ref.set("GEEKS FOR GEEKS");
// get and print value
String value = ref.get();
System.out.println("V = " + value);
}
}
输出:
V = GEEKS FOR GEEKS
参考: https: Java/util/concurrent/atomic/AtomicReference.html#get()