当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。