Skip to main content

Posts

Showing posts from January 19, 2019

Java Concurrency - Atomic classes

Greetings! Java let us use update operations on single variables in non-blocking, thready safe programming with Atomic variables. ( atomic-package-summary ) This is like a enhanced version of volatile variables. It should be note that these classes are not replacement for Integer and related classes. These are not only thread safe, but also provides better performance than using synchronized blocks. Source code for Synchronized vs Atomic classes performance can be found here . Main classes, AtomicBoolean AtomicInteger AtomicLong AtomicReference Say we want a counter, AtomicInteger and AtomicLong has below methods. incrementAndGet() - Atomically increments by one the current value. decrementAndGet() - Atomically decrements by one the current value. import java.util.concurrent.atomic.AtomicInteger; public class AtomicCounter implements Counter { private AtomicInteger atomicInteger = new AtomicInteger(0); public void increment() { atomicInteger