Java OpenResty Spring Spring Boot MySQL Redis MongoDB PostgreSQL Linux Android Nginx 面试 小程序 Arthas JVM AQS juc Kubernetes Docker DevOps


Java 并发编程之 AtomicBoolean

Java juc 大约 560 字

代码

public class AtomicBooleanDemo {

    public static void main(String[] args) {
        AtomicBoolean atomicBoolean = new AtomicBoolean();
        boolean b = atomicBoolean.get();
        atomicBoolean.compareAndSet(b, true);
    }

}

源码解析

AtomicBoolean底层是使用int类型字段,用1表示true0表示false

compareAndSet底层源码。

private volatile int value;

public final boolean compareAndSet(boolean expectedValue, boolean newValue) {
    return VALUE.compareAndSet(this,
                               (expectedValue ? 1 : 0),
                               (newValue ? 1 : 0));
}
阅读 707 · 发布于 2021-09-20

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb

扫描下方二维码关注公众号和小程序↓↓↓

扫描二维码关注我
昵称:
随便看看 换一批