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


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


AtomicReference類的setRelease()方法用於設置具有 memory 效應的此AtomicReference對象的值,該效應由VarHandle.setRelease(java.lang.Object…)指定。此方法具有與memory_order_release排序兼容的內存排序效果。

用法:

public final void setRelease(V newValue)

參數:此方法接受newValue,它是要設置的新值。


返回值:此方法不返回任何內容。

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

// Java program to demonstrate 
// AtomicReference.setRelease() method 
  
import java.util.concurrent.atomic.AtomicReference; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an atomic reference object. 
        AtomicReference<Long> ref 
            = new AtomicReference<Long>(); 
  
        // set some value using setRelease method 
        ref.setRelease(123457876L); 
  
        // print value 
        System.out.println("value = " + ref.get()); 
    } 
}
輸出:

程序2:

// Java program to demonstrate 
// AtomicReference.setRelease() method 
import java.util.concurrent.atomic.AtomicReference; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an atomic reference object 
        AtomicReference<String> ref 
            = new AtomicReference<String>(); 
  
        // set some value using setRelease() 
        ref.setRelease("String Builder"); 
  
        // print value 
        System.out.println("Class = " + ref.get()); 
    } 
}
輸出:

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



相關用法


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