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:
- Very fast completion of reads and writes
- Reads may or may not return latest result but should not return corrupted result
- 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:
- primitive long with volatile keyword
- AtomicLong
- LongAccumulator with accumulatorFunction being
(x,y) -> y
- 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
Use
AtomicLong
as it helps to avoid additional overhead of explicit locking