AtomicReference類的get()方法用於以讀取的內存語義返回此AtomicReference對象的值,就好像該變量被聲明為變量的易失性類型一樣。
用法:
public final V get()
參數:此方法不接受任何內容。
返回值:此方法返回此AtomicReference的當前值。
以下示例程序旨在說明get()方法:
程序1:
// 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<Integer> ref
= new AtomicReference<Integer>();
// set some value
ref.set(12);
// get and print value
int value = ref.get();
System.out.println("value = " + value);
}
}
輸出:
value = 12
程序2:
// 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<String> ref
= new AtomicReference<String>();
// 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://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReference.html#get()
相關用法
- Java AtomicReference set()用法及代碼示例
- Java AtomicReference getAcquire()用法及代碼示例
- Java AtomicReference getAndSet()用法及代碼示例
- Java AtomicReference setRelease()用法及代碼示例
- Java AtomicReference accumulateAndGet()用法及代碼示例
- Java AtomicReference lazySet()用法及代碼示例
- Java AtomicReference compareAndSet()用法及代碼示例
- Java AtomicReference getAndUpdate()用法及代碼示例
- Java AtomicReference getPlain()用法及代碼示例
- Java AtomicReference compareAndExchange()用法及代碼示例
- Java AtomicReference setOpaque()用法及代碼示例
- Java AtomicReference updateAndGet()用法及代碼示例
- Java AtomicReference getOpaque()用法及代碼示例
- Java AtomicReference setPlain()用法及代碼示例
- Java AtomicReference weakCompareAndSetPlain()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 AtomicReference get() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。