In this blog post we will discuss about one of the very important java interview question When we choose LongAdder and LongAccumulator over AtomicLong & AtomicInteger ?
- In case of when you have a very large amount of threads accessing the same atomic variable, performance suffers because the optimistic updates require too many retries. Java 8 came up with classes LongAdder and LongAccumulator to solve this problem.
- A LongAdder class is from concurrent package is composed of multiple variables whose collective sum is the current value. When multiple threads can update different summands and new summands are automatically provided when the number of threads increases.
- This is efficient in the common situation where the value of the sum is not needed until after all work has been done. The performance improvement can be substantial.
You may also like:
What are common multi-threading issues faced by Java Developers?
What are different states of a Thread? What does those states tell us?
What happens when wait() & notify() methods are called?
What is difference between sleep(), yield() and wait() method?
What is difference between Callable and Runnable Interface?
What is difference between intrinsic synchronization and explicit locking using Lock?
What will happen when an exception arises from within a synchronized code block? Will lock be retained or released?
What is difference between Executor submit() and execute() method?
That’s all about When we choose LongAdder and LongAccumulator over AtomicLong & AtomicInteger ?
If you have any feedback or suggestion please feel free to drop in blow comment box.