Java Reference
In-Depth Information
Chapter 15. Atomic Variables and Nonblocking
Synchronization
Many of the classes in java.util.concurrent , such as Semaphore and Concur-
rentLinkedQueue , provide better performance and scalability than alternatives using
synchronized . In this chapter, we take a look at the primary source of this performance
boost: atomic variables and nonblocking synchronization.
Much of the recent research on concurrent algorithms has focused on nonblocking algorithms ,
which use low-level atomic machine instructions such as compare-and-swap instead of locks
to ensure data integrity under concurrent access. Nonblocking algorithms are used extensively
in operating systems and JVMs for thread and process scheduling, garbage collection, and to
implement locks and other concurrent data structures.
Nonblocking algorithms are considerably more complicated to design and implement than
lock-based alternatives, but they can offer significant scalability and liveness advantages. They
coordinate at a finer level of granularity and can greatly reduce scheduling overhead because
they don't block when multiple threads contend for the same data. Further, they are immune
to deadlock and other liveness problems. In lock-based algorithms, other threads cannot make
progress if a thread goes to sleep or spins while holding a lock, whereas nonblocking al-
gorithms are impervious to individual thread failures. As of Java 5.0, it is possible to build ef-
ficient nonblocking algorithms in Java using the atomicvariableclasses such as AtomicIn-
teger and AtomicReference .
Atomic variables can also be used as “better volatile variables” even if you are not developing
nonblocking algorithms. Atomic variables offer the same memory semantics as volatile vari-
ables, but with additional support for atomic updates—making them ideal for counters, se-
quence generators, and statistics gathering while offering better scalability than lock-based al-
ternatives.
15.1. Disadvantages of Locking
Coordinating access to shared state using a consistent locking protocol ensures that whichever
thread holds the lock guarding a set of variables has exclusive access to those variables, and
that any changes made to those variables are visible to other threads that subsequently acquire
the lock.
Search WWH ::




Custom Search