Recently I was reading a tutorial, in that I came across a statement that says..
"The Java language specification guarantees that reading or writing a variable is an atomic operation(unless the variable is of type long
or double
). Operations variables of type long
or double
are only atomic if they declared with the volatile
keyword."
AtomicInteger
or AtomicLong
that provides methods like getAndDecrement()
, getAndIncrement()
and getAndSet()
which are atomic.
I got confused a bit with the above statement.. could you please clarify when to use AtomicInteger
or AtomicLong
classes.