Search code examples
javathread-safety

Is a java boolean thread safe if you synchronize the writes only


Is a class with synchronized writes on an instance variable boolean thread safe? E.g.:

boolean foo = true;

public synchronized void setFoo(boolean foo) {
  this.foo = foo;
}

public boolean getFoo() {
  return foo;
}

Solution

  • No, not thread-safe.

    You’ve not considered the problem of visibility.

    Changes to the primitive boolean are not necessarily visible across threads. Because of the ways modern CPUs work, optimizations made by the JIT compiler, and the Java Memory Model, one thread may not see the changes made by another thread.

    volatile

    One way to solve the visibility problem is to mark the primitive boolean variable as volatile.

    AtomicBoolean

    Another way is to replace your AtomicBoolean object instead of the primitive boolean. Mark the AtomicBoolean variable as final to guard against inadvertently assigning another instance.

    final AtomicBoolean atomicFoo = new AtomicBoolean ( true ) ;
    

    Then use the getter/setter methods to access and manipulate the boolean payload kept within that object in a thread-safe manner.

    Personally, I prefer the Atomic… classes. Using these classes means the programmer must call methods rather than merely make assignments. This phenomenon is a constant reminder to the programmer that they are operating in a multi-threaded situation.

    Regarding existing code, the compiler will be an enormous help in refactoring to use AtomicBoolean. Every place that primitive was accessed or manipulated will be highlighted as a compiler error requiring your attention.