Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 616
Marcin Avatar asked Dec 05 '12 13:12

Marcin


People also ask

What is Java Util Concurrent Atomic?

concurrent. atomic. A small toolkit of classes that support lock-free thread-safe programming on single variables. Instances of Atomic classes maintain values that are accessed and updated using methods otherwise available for fields using associated atomic VarHandle operations.

What is the use of AtomicBoolean in Java?

AtomicBoolean class provides operations on underlying boolean value that can be read and written atomically, and also contains advanced atomic operations. AtomicBoolean supports atomic operations on underlying boolean variable. It have get and set methods that work like reads and writes on volatile variables.

What is an atomic integer Java?

An AtomicInteger is used in applications such as atomically incremented counters, and cannot be used as a replacement for an Integer . However, this class does extend Number to allow uniform access by tools and utilities that deal with numerically-based classes.


1 Answers

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.

like image 165
Peter Lawrey Avatar answered Oct 23 '22 08:10

Peter Lawrey