AtomicVariables penalty for blocking in Java

In my current project, I'm trying to use Atomic Integer and Atomic Booleans whenever possible when we have more than 1 thread accessing it. This helps keep the logical lock loose (internally I know it can still use locks) and the code is much cleaner. The use case is mainly for configuration tags that can change dramatically.

I want to know what is the penalty efficiency when using Atomic Variables, would that invalidate the cache too often and actually make my solution worse than just using locks?

+3


source to share


1 answer


atomic classes do not use locks, it uses CAS (compare-and-set) for thread safety. they are generally faster than the blocking option, but they tend to be slower when there is very high contention. if you want some analogy it is something like optimistic and pessimistic locking in DB.

if you're interested in more details, you can check out Java Performance: The Definitive Guide .



ADD . With cache, I am assuming you mean the relationship between events and relationships. Here is a quote from the oracle tutorial :

The java.util.concurrent.atomic package defines classes that support atomic operations on single variables. All classes get and set methods that act like reading and writing to volatile variables. That is, the set has a relationship before the same variable happens to any subsequent ones.

0


source







All Articles