简体   繁体   中英

Atomic assignment of long value in java

I have the use case to update epoch time of events in a long variable. This variable will have lots of concurrent reads and writes as well. Here are the requirements in detail:

  1. Very fast completion of reads and writes
  2. Reads may or may not return latest result but should not return corrupted result
  3. Writes are simple assignment to a new epoch value, no addition, subtraction or calculation is required

Which of the alternatives is a better option for my use case:

  1. primitive long with volatile keyword
  2. AtomicLong
  3. LongAccumulator with accumulatorFunction being (x,y) -> y
  4. Two different variables - one for only reading values and other a volatile variable just for writing value, the value of write variable one being copied to read variable in some interval

使用AtomicLong因为它有助于避免显式锁定的额外开销

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM