Can some one explain what AtomicLong is used for? For example, what's the difference in the below statements?
private Long transactionId;
private AtomicLong transactionId;
Can some one explain what AtomicLong is used for? For example, what's the difference in the below statements?
private Long transactionId;
private AtomicLong transactionId;
There are significant differences between these two objects, although the net result is the same, they are definitely very different and used under very different circumstances.
You use a basic Long
object when:
You use an AtomicLong
when:
Long
by itself doesn't allow for thread interopability since two threads could both see and update the same value, but with an AtomicLong
, there are pretty decent guarantees around the value that multiple threads will see.
Effectively, unless you ever bother working with threads, you won't need to use AtomicLong
.