📜  Java中的 AtomicBooleanlazySet() 方法及示例

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

Java中的 AtomicBooleanlazySet() 方法及示例

Java.util.concurrent.atomic.AtomicBoolean.lazySet()是Java中的一个内置方法,它更新先前的值并将其设置为在参数中传递的新值。

句法:

public final void lazySet(boolean newVal)

参数:该函数接受一个要更新的强制参数newVal

返回值:该函数不返回任何内容。

下面的程序说明了上述函数:

方案一:

// Java program that demonstrates
// the lazySet() function
  
import java.util.concurrent.atomic.AtomicBoolean;
  
public class GFG {
    public static void main(String args[])
    {
  
        // Initially value as false
        AtomicBoolean val
            = new AtomicBoolean(false);
  
        System.out.println("Previous value: "
                           + val);
  
        val.lazySet(true);
  
        // Prints the updated value
        System.out.println("Current value: "
                           + val);
    }
}
输出:
Previous value: false
Current value: true

方案二:

// Java program that demonstrates
// the lazySet() function
  
import java.util.concurrent.atomic.AtomicBoolean;
  
public class GFG {
    public static void main(String args[])
    {
  
        // Initially value as true
        AtomicBoolean val
            = new AtomicBoolean(true);
  
        System.out.println("Previous value: "
                           + val);
  
        val.lazySet(false);
  
        // Prints the updated value
        System.out.println("Current value: "
                           + val);
    }
}
输出:
Previous value: true
Current value: false

参考: https: Java/util/concurrent/atomic/AtomicBoolean.html#lazySet-boolean-