Java 并发编程之 AtomicBoolean

Java juc About 560 words

代码

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));
}
Views: 1,662 · Posted: 2021-09-20

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

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

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


Today On History
Browsing Refresh