AtomicBoolean是原子变量在这个Boolean值的变化的时候不允许在之间插入,保持操作的原子性.
多线程会一起工作:
private static boolean exists = false;
一个线程工作
private static AtomicBoolean exists = new AtomicBoolean(false);
exists.compareAndSet(false, true)
在线程中使用。
compareAndSet(expect,update)理解如下:
if (this == expect) {
this = update
return true;
} else {
return false;
}