Suppose I have the following code:
AtomicBoolean condition;
condition = new AtomicBoolean(false);
(...)
while(!condition.get()){
// do some stuff
}
I know that condition.get()
is atomic, but, is !condition.get()
atomic as well?
I mean, could it happen that one Thread
read the boolean value atomically and then it were interrupted before applying the !
operation, so that another Thread
gets into the loop before? If that's the case, would it be better to use a function such as:
private synchronized boolean checkNegatedCondition(AtomicBoolean cond){
return !cond.get();
}
(...)
while(checkNegatedCondition(condition)){
// do some stuff
}
Thanks in advance.
One has to be precise here:
get()
operation is supposed to be atomic, and because of the "further" guarantees for any atomic class, it is also guaranteed to be "visible" to other threads looking at the same object (atomic in the sense that all the **set* methods are atomic, so it is not possible to get()
something that is "half-set")And just to be sure: the core thing here is not "atomicity" - because a get()
just returns a value (and in contrast to a double, there is even just a single bit/byte affected). In other words: AtomicBoolean
is mainly about making sure that changes to the object are visible to all threads using the object.
Buty yes, assuming that AtomicBoolean is a field of a class, and multiple threads are calling a method like:
public void foo() {
while(!condition.get() ...
it is theoretically possible that
But: assume that we had a "protected" getNot()
, the final result would be the same:
In both cases, the first thread would enter the loop!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With