當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java AtomicReference getAcquire()用法及代碼示例


AtomicReference類的getAcquire()方法用於返回此AtomicReference對象的值,並且對獲取變量的內存排序效果與memory_order_acquire排序兼容。

用法:

public final V getAcquire()

參數:此方法不接受任何內容。


返回值:此方法返回此AtomicReference對象的當前值,並具有內存排序效果。

以下示例程序旨在說明get()方法:

程序1:

// Java program to demonstrate 
// AtomicReference.getAcquire() 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(1632798372); 
  
        // apply getAcquire() and print value 
        int value = ref.getAcquire(); 
        System.out.println("value  = " + value); 
    } 
}
輸出:

程序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("WELCOME TO "
                + "GEEKS FOR GEEKS"
                + " LERAN AND APPLY"); 
  
        // apply getAcquire and print value 
        String value = ref.getAcquire(); 
        System.out.println(value); 
    } 
}
輸出:

參考: https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReference.html#getAcquire()



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 AtomicReference getAcquire() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。