Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it preferable to use volatile boolean in Java rather than AtomicBoolean? [duplicate]

I've looked at the other volatile vs. Atomicxxxx questions in SO (including this one) and have read the description of java.util.current.atomic, and I am not quite satisfied with the nuances.

If I'm trying to decide between using volatile boolean and AtomicBoolean, are there practical differences besides the atomic read-modify-write operations offered by AtomicBoolean? (e.g. compareAndSet() and getAndSet())

Suppose I have

volatile boolean flag; 

Then one or more threads set the flag (but not clear it). If I have one thread that reads the flag, and if set, does an action, and then clears the flag, is volatile adequate?

Is there a higher cost to AtomicBoolean than volatile boolean, in terms of

  • memory space
  • performance hit (volatile boolean appears to require memory fencing, AtomicBoolean appears to require memory fencing + some minor locking on CAS operations as per the java.util.current.atomic description)

My gut call is to just go with AtomicBoolean and be safe, but I want to understand if there's ever a situation to use volatile boolean instead (e.g. if I had thousands of instances of them and performance were an issue).

like image 452
Jason S Avatar asked Feb 02 '11 15:02

Jason S


People also ask

Does AtomicBoolean need to be volatile?

If the boolean value is read by many threads, but written by only one thread, then volatile boolean is sufficient. If there are also many writers, you may need AtomicBoolean .

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.

How does volatile work in Java?

Volatile keyword is used to modify the value of a variable by different threads. It is also used to make classes thread safe. It means that multiple threads can use a method and instance of the classes at the same time without any problem. The volatile keyword can be used either with primitive type or objects.

How does AtomicBoolean compare?

The method compareAndSet() allows you to compare the current value of the AtomicBoolean to an expected value, and if current value is equal to the expected value, a new value can be set on the AtomicBoolean . The compareAndSet() method is atomic, so only a single thread can execute it at the same time.


1 Answers

The main difference between AtomicBoolean and volatile from a practical point of view is that the compare-and-set operation isn't atomic with volatile variables.

 volatile boolean b;   void foo() {    if( b ) {      //Here another thread might have already changed the value of b to false      b = false;    }  } 

But seeing as all your concurrent writes are idempotent and you only read from one thread, this shouldn't be a problem.

like image 108
biziclop Avatar answered Oct 26 '22 20:10

biziclop