Home > Back-end >  2020-10-18: what is the difference between LongAdder and AtomicLong in Java?
2020-10-18: what is the difference between LongAdder and AtomicLong in Java?

Time:10-19

2020-10-18: what is the difference between LongAdder and AtomicLong in Java? # # f greatly architects a daily topic

CodePudding user response:

AtomicLong: AtomicLong is based on CAS way to spin the updates, the only reason is that can restrict AtomicLong efficient high concurrency, high concurrency means CAS more likely to fail, and try again more, the more multi-threaded retry, CAS failure probability and the higher, into a vicious cycle, AtomicLong efficiency is lower, AtomicLong include atomic read, write API,

LongAdder: LongAdder is to divide the value into several cell, concurrency value low, CAS updated value directly, success is over, the condition of high concurrency, CAS update a data needed for the cell capacity and cell values, successful end; Update failed spin CAS to update the cell value, value, called sum () method for each cell accumulation, LongAdder without atomic read, write API, can guarantee results eventual consistency,

CodePudding user response:

reference 1st floor also night reply:
AtomicLong: AtomicLong is based on CAS way to spin the updates, the only reason is that can restrict AtomicLong efficient high concurrency, high concurrency means CAS more likely to fail, and try again more, the more multi-threaded retry, CAS failure probability and the higher, into a vicious cycle, AtomicLong efficiency is lower, AtomicLong include atomic read, write API,

LongAdder: LongAdder is to divide the value into several cell, concurrency value low, CAS updated value directly, success is over, the condition of high concurrency, CAS update a data needed for the cell capacity and cell values, successful end; Update failed spin CAS to update the cell value, value, called sum () method for each cell accumulation, LongAdder without atomic read, write API, can guarantee results eventual consistency,
bosses, summarized in place!

CodePudding user response:

AtomicLong is the CAS operation, LongAdder is multiple unit operation,
  • Related