What is the difference between using a volatile primitive over atomic variables?
concurrency, java
Solution
The visibility semantics are exactly the same, the situation where using the atomic primitives is useful is when you need to use their atomic methods.
For example:
if (volatileBoolean) {
volatileBoolean = !volatileBoolean;
}
could create issues in a multi threaded environment as the variable could change between the two lines. If you need the test&assignment to be atomic, you can use:
atomicBoolean.compareAndSet(true, false);
Problem
Possible Duplicate: Java: volatile boolean vs AtomicBoolean When is it appropriate to use a `volatile` primitive (e.g. `boolean`, `integer` or `long`) instead of `AtomicBoolean`, `AtomicInteger` or `AtomicLong`, and vice-versa?