atomicboolean

AtomicBoolean vs synchronized block

拥有回忆 提交于 2019-12-21 11:04:48
问题 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

AtomicBoolean vs synchronized block

前提是你 提交于 2019-12-04 05:07:15
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 on synchronization so I ran a little micro-benchmark . For 10 concurrent threads and 1000000 iterations

Difference between getAndSet and compareAndSet in AtomicBoolean

旧街凉风 提交于 2019-11-30 16:42:54
问题 The thread title should be self-explnatory... I'm a bit confused between the specification of below methos from AtomicBoolean class: java.util.concurrent.atomic.AtomicBoolean#compareAndSet java.util.concurrent.atomic.AtomicBoolean#getAndSet My assemption is that both would result in the same behavior when used as a boolean clause in an if condition: public class Test { private AtomicBoolean flag = AtomicBoolean(false); public void processSomeAction() { if (flag.getAndSet(false)) { // Shouldn

Volatile boolean vs AtomicBoolean

风流意气都作罢 提交于 2019-11-26 05:54:36
What does AtomicBoolean do that a volatile boolean cannot achieve? They are just totally different. Consider this example of a volatile integer: volatile int i = 0; void incIBy5() { i += 5; } If two threads call the function concurrently, i might be 5 afterwards, since the compiled code will be somewhat similar to this (except you cannot synchronize on int ): void incIBy5() { int temp; synchronized(i) { temp = i } synchronized(i) { i = temp + 5 } } If a variable is volatile, every atomic access to it is synchronized, but it is not always obvious what actually qualifies as an atomic access.

Volatile boolean vs AtomicBoolean

岁酱吖の 提交于 2019-11-26 01:48:32
问题 What does AtomicBoolean do that a volatile boolean cannot achieve? 回答1: They are just totally different. Consider this example of a volatile integer: volatile int i = 0; void incIBy5() { i += 5; } If two threads call the function concurrently, i might be 5 afterwards, since the compiled code will be somewhat similar to this (except you cannot synchronize on int ): void incIBy5() { int temp; synchronized(i) { temp = i } synchronized(i) { i = temp + 5 } } If a variable is volatile, every atomic