📌  相关文章
📜  Java中的 AtomicReference getAcquire() 方法及示例

📅  最后修改于: 2022-05-13 01:55:40.137000             🧑  作者: Mango

Java中的 AtomicReference getAcquire() 方法及示例

AtomicReference类的getAcquire()方法用于返回此 AtomicReference 对象的值,对获取变量的内存排序效果与 memory_order_acquire 排序兼容。

句法:

public final V getAcquire()

参数:此方法不接受任何内容。
返回值:此方法返回此 AtomicReference 对象的当前值,具有内存排序效果。

下面的程序说明了 get() 方法:

方案一:

Java
// 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 ref
            = new AtomicReference();
 
        // set some value
        ref.set(1632798372);
 
        // apply getAcquire() and print value
        int value = ref.getAcquire();
        System.out.println("value  = " + value);
    }
}


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


输出:

方案二:

Java

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

输出:

参考: https: Java/util/concurrent/atomic/AtomicReference.html#getAcquire()