AtomicBoolean vs synchronized block
问题 I was trying to cut thread contention in my code by replacing some synchronized blocks with AtomicBoolean . Here's an example with synchronized : public void toggleCondition() { synchronized (this.mutex) { if (this.toggled) { return; } this.toggled = true; // do other stuff } } And the alternative with AtomicBoolean : public void toggleCondition() { if (!this.condition.getAndSet(true)) { // do other stuff } } Taking advantage of AtomicBoolean 's CAS property should be way faster than relying