Java AtomicBoolean lazySet()方法及示例
java.util.concurrent.atomic.AtomicBoolean.lazySet() 是java中的一个内置方法,它可以更新之前的值并将其设置为参数中传递的新值。
语法
public final void lazySet(boolean newVal)
参数: 该函数接受一个要更新的强制性参数 newVal 。
返回值: 该函数不返回任何东西。
以下程序说明了上述函数。
程序1 :
// 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
程序2
// 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://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicBoolean.html#lazySet-boolean-
极客教程