Is there any performance difference between AtomicInteger
and Integer
?
Bookmark this question. Show activity on this post. I testes how fast the Atomic Integer in multithread with comparing synchronized method, but I got the result that Atomic Integer was slower than synchronized method.
Integers are object representations of literals and are therefore immutable, you can basically only read them. AtomicIntegers are containers for those values. You can read and set them. Same as asigning a value to variable.
An AtomicInteger is used in applications such as atomically incremented counters, and cannot be used as a replacement for an Integer . However, this class does extend Number to allow uniform access by tools and utilities that deal with numerically-based classes. Since: 1.5 See Also: Serialized Form.
This is source code of AtomicInteger. The value is Volatile. So,AtomicInteger uses Volatile inside.
The choice of these two types should not depend on the performance. The main choice for AtomicInteger
is if you want to achieve thread safety with the operations on the integer.
However the performace difference might strongly depend on the choosen operating system, as the detailed implementation of atomic operations depend on the operating system.
AtomicInteger
allows some (not all!) operations that would otherwise require synchronization to be performed in a lock-free manner using special hardware instructions. How this affects performance is somewhat complex:
AtomicInteger
will probably be implemented using synchronization.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With