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

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

Java中的 AtomicLong get() 方法及示例

Java.util.concurrent.atomic.AtomicLong.get()是Java中的一个内置方法,它返回日期类型为 long 的当前值。

句法:

public final long get()

参数:该函数不接受任何参数。

返回值:函数返回当前值

下面的程序说明了上述方法:

方案一:

// Java program that demonstrates
// the get() function
  
import java.util.concurrent.atomic.AtomicLong;
  
public class GFG {
    public static void main(String args[])
    {
  
        // Initially value as 0
        AtomicLong val = new AtomicLong(0);
  
        val.addAndGet(7);
  
        // Prints the updated value
        System.out.println("Previous value: "
                           + val);
  
        // Gets the current value
        long res = val.get();
  
        System.out.println("current value: "
                           + res);
    }
}
输出:
Previous value: 7
current value: 7

方案二:

// Java program that demonstrates
// the get() function
  
import java.util.concurrent.atomic.AtomicLong;
  
public class GFG {
    public static void main(String args[])
    {
  
        // Initially value as 18
        AtomicLong val = new AtomicLong(18);
  
        val.addAndGet(7);
  
        // Prints the updated value
        System.out.println("Previous value: "
                           + val);
  
        // Gets the current value
        long res = val.get();
  
        System.out.println("current value: "
                           + res);
    }
}
输出:
Previous value: 25
current value: 25

参考: https: Java/util/concurrent/atomic/AtomicLong.html#get–