- Forward


Atomic Variables
in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • Review:
    • Shared mutable state variables can be protected using synchronized methods/blocks and other coordination mechanisms
  • An Observation:
    • Such methods are less efficient than non-blocking algorithms that use low-level atomic machine instructions
Motivation (cont.)
Back SMYC Forward
  • Pessimistic Methods:
    • Assume the worst and don't proceeed until it can be guaranteed that other threads won't cause problems
  • Optimistic Methods:
    • Perform an update hoping that it can be completed and then check for interference
Motivation (cont.)
Back SMYC Forward
  • Support on CPUs:
    • Many CPUs now support such instructions (e.g., a compare-and-swap instruction; load-linked and store-conditional instructions)
  • Support in the Java Virtual Machine:
    • The JVM uses these instructions when available and simulates them efficiently when not
Atomic Variable Classes
Back SMYC Forward
  • Purpose:
    • Support atomic conditional read-modify-write operations
  • The Classes:
    • AtomicBoolean
    • AtomicInteger
    • AtomicLong
  • The Important Methods:
    • addAndGet(), decrementAndGet(), incrementAndGet()
    • getAndAdd(), getAndDecrement(), getAndIncrement()
    • compareAndSet(), weakCompareAndSet()
    • get(), lazySet(), set()
An Example Revisited
Back SMYC Forward
  • Recall:
    • We created a NonatomicInteger class to demonstrate the problems that can arise when using shared mutable state
    • We created a SynchronizedInteger class to demonstrate how these problems could be overcome with synchronization
  • Now:
    • We're going to use an AtomicInteger instead
An Example Revisited (cont.)
Back SMYC Forward

The Counter Class

javaexamples/atomic/Counter.java
 
An Example Revisited (cont.)
Back SMYC Forward

The Application

javaexamples/atomic/CounterDriver.java
 
Adders
Back SMYC Forward
  • Purpose:
    • Used when multiple threads update a common sum
  • Classes:
    • DoubleAdder
    • LongAdder
  • Comparison to AtomicLong:
    • Under high contention, expected throughput is significantly higher, at the expense of higher space consumption
Atomic Arrays
Back SMYC Forward
  • The Classes:
    • AtomicIntegerArray
    • AtomicLongArray
  • The Methods:
    • Similar to those in AtomicInteger and AtomicLong but they have a parameter for the element index
The AtomicReference Class
Back SMYC Forward
  • Purpose:
    • An object reference that can be updated atomically
  • The Most Common Use:
    • Concurrent collections
  • Examples:
    • A nonblocking stack (e.g., using Treiber's algorithm)
    • ConcurrentLinkedQueue
    • ConcurrentLinkedDeque
    • ConcurrentSkipListMap
What About Atomic Floating Point Numbers?
Back SMYC Forward
  • Existing Classes:
    • There aren't any other than the DoubleAdder (for a variety of reasons)
  • A Workaround:
    • static long Double.doubleToLongBits(double value)
    • static double Double.longBitsToDouble(long bits)
There's Always More to Learn
Back -