Card 08/ 09

ExerciseDifficulty: Advanced1 min

Practice: Choosing volatile, an Atomic Class or a Lock for Each Field

A flag, a counter, a shared map and two fields that must agree.

volatile fixes visibility, an atomic class makes one value's read-compute-write atomic, and a lock covers several fields at once. For each field below, decide what it needs (nothing, volatile, an atomic class, or a lock) and why, all four before you open the answer.

The four fields
The field and how it is used
1boolean shutdown, written once by a signal handler and read by every worker loop
2long requestsHandled, incremented by every request thread
3final Map<String, Session> sessions, put into and read by many threads
4long balance and long lastMovement, which must always agree with each other
What each of the four needs
Each field, what it needs, and why
NeedsWhy
1volatileWritten whole, never computed from itself, so visibility is the only problem
2An AtomicLongRead, add, write back: volatile would make it visible and still lose updates
3A ConcurrentHashMapfinal publishes the reference safely; the map's contents still need a thread-safe implementation
4A lock, held across both writesTwo fields that must agree, which no per-field tool can span

The third is the one to pause on. final guarantees that every thread sees the reference once the constructor finishes, and says nothing about what happens inside the object it points at.

The fourth marks the limit of the other tools. An atomic class covers one variable, and once correctness spans two fields the only tool left is a lock.