How LongAdder performs better than AtomicLong

longadder example
longaddr
java atomicinteger alternative
java concurrent counter
how atomicinteger works in java
concurrent accumulators in java 8
adder class in java
atomic integer

I see how Java's AtomicInteger works internally with CAS (Compare And Swap) operation. Basically when multiple threads try to update the value, JVM internally use the underlying CAS mechanism and try to update the value. If the update fails, then try again with the new value but never blocks.

In Java8 Oracle introduced a new Class LongAdder which seems to perform better than AtomicInteger under high contention. Some blog posts claim that LongAdder perform better by maintaining internal cells - does that mean LongAdder aggregates the values internally and update it later? Could you please help me to understand how LongAdder works?

does that mean LongAdder aggregates the values internally and update it later?

Yes, if I understand your statement correctly.

Each Cell in a LongAdder is a variant of an AtomicLong. Having multiple such cells is a way of spreading out the contention and thus increasing throughput.

When the final result (sum) is to be retrieved, it just adds together the values of each cell.

Much of the logic around how the cells are organized, how they are allocated etc can be seen in the source: http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/f398670f3da7/src/java.base/share/classes/java/util/concurrent/atomic/Striped64.java

In particular the number of cells is bound by the number of CPUs:

/** Number of CPUS, to place bound on table size */
static final int NCPU = Runtime.getRuntime().availableProcessors();

java - How LongAdder performs better than AtomicLong, Java 8 Performance Improvements: LongAdder vs AtomicLong More impressive is that LongAdder's performance is constant until the number  How LongAdder performs better than AtomicLong. Ask Question Asked 4 years, 7 months ago. Active 8 months ago. Viewed 7k times 20. 6. I see how Java's AtomicInteger

The primary reason it is "faster" is its contended performance. This is important because:

Under low update contention, the two classes have similar characteristics.

You'd use a LongAdder for very frequent updates, in which atomic CAS and native calls to Unsafe would cause contention. (See source and volatile reads). Not to mention cache misses/false sharing on multiple AtomicLongs (although I have not looked at the class layout yet, there doesn't appear to be sufficient memory padding before the actual long field.

under high contention, expected throughput of this class is significantly higher, at the expense of higher space consumption.

The implementation extends Striped64, which is a data holder for 64-bit values. The values are held in cells, which are padded (or striped), hence the name. Each operation made upon the LongAdder will modify the collection of values present in the Striped64. When contention occurs, a new cell is created and modified, so the the old thread can finish concurrently with contending one. When you need the final value, the sums of each cell is simply added up.

Unfortunately, performance comes with a cost, which in this case is memory (as often is). The Striped64 can grow very large if a large load of threads and updates are being thrown at it.

Quote source: Javadoc for LongAdder

Java 8 Performance Improvements: LongAdder vs AtomicLong , How LongAdder performs better than AtomicLong. Question. I see how Java's AtomicInteger works internally with CAS (Compare And Swap) operation. How LongAdder performs better than AtomicLong. Tag: java,concurrency. I see how Java's AtomicInteger works internally with CAS (Compare And Swap) operation. Basically

Atomic Long uses CAS which - under heavy contention can lead to many wasted CPU cycles. LongAdder, on the other hand, uses a very clever trick to reduce contention between threads, when these are incrementing it. So when we call increment() , behind the scenes LongAdder maintains an array of counter that can grow on demand. And so, when more threads are calling increment(), the array will be longer. Each record in the array can be updated separately – reducing the contention. Due to that fact, the LongAdder is a very efficient way to increment a counter from multiple threads. The result of the counter in the LongAdder is not available until we call the sum() method.

How LongAdder performs better than AtomicLong, Java 8 LongAdders: The Right Way To Manage Concurrent Counters some of the options Java offered up until now, and how they perform compared to this new API. If more than one thread is doing this at the same time for the same AtomicInteger – this set of classes uses CAS (compare-and-swap)  With a single thread, the new LongAdder is one third slower, but when threads are in contention to increment the field, LongAdder shows its value. Note that the only thing each thread is doing is attempting to increment the counter—this is a synthetic benchmark of the most extreme kind.

Java 8 LongAdders: The Fastest Way To Add Numbers Concurrently, First, how well does LongAdder perform? I downloaded AtomicLong does a much better job at this than synchronized, as expected. However  LongAdder is a new atomic class in Java 8. In multithreaded environments, LongAdder performs much better than AtomicLong, especially in more-written scenarios. How did it come about? Let's study together. principle. The principle of LongAdder is that when there is no competition at first, only the value of base is updated.

Java 8 Concurrency: LongAdder, Learn how LongAdder and LongAccumulator can aid you in your some values very often, where using an AtomicLong can be a bottleneck. And so, when more threads are calling increment(), the array will be longer. results according to the supplied LongBinaryOperator – this works similarly to the  Most improvements like the LongAdder actually come from libraries, which are then recommended for the core language through the JSR process. JMX, for example, was JSR 3. Java 8 itself is JSR 337. If, in the past, you've been very performance-conscious about this stuff, you're probably using stuff like Trove, HPPC for collections.

LongAdder and LongAccumulator in Java, Advice for the concurrently confused: AtomicLong JDK7/8 vs. NO contention is present, AtomicLong performs slightly better than LongAdder. When NO contention is present, AtomicLong performs slightly better than LongAdder. To avoid contention LongAdder will allocate Cells (see previous post for implementation discussion) each Cell will consume at least 256 bytes (current implementation of @Contended) and you may have as many Cells as CPUs. If you are on a tight memory budget and have allot of counters this is perhaps not the tool for the job.

Comments
  • stackoverflow.com/questions/27628538/…
  • At first I think I misunderstood you. Reading your question again, I think you got the idea right.
  • Thanks ! I'm more interested in understanding how these internal cells are organized? say if 100 threads are trying to update the value, how many internal cells are created and how they are updated?
  • For such questions regarding implementation details, I would suggest you refer to the source. It's actually quite easy to read and understand. See my updated answer for a link to the latest revision.