Search code examples
javamultithreadingconcurrencyatomicjava.util.concurrent

Why java.util.concurrent.atomic.AtomicBoolean is internally implemented with int?


AtomicBoolean stores its value in:

private volatile int value;

Then, for example, extracting its value is done like this:

    public final boolean get() {
    return value != 0;
}

What is the reason behind it? Why boolean was not used?


Solution

  • AFAIK, int is the smallest type CAS operations can be implemented across different machine types.

    Note: as object allocations are 8 byte aligned, using a smaller type wouldn't save any memory.