About this tutorial
JDK 5.0 is a major step forward for the creation of highly scalable concurrent applications in the Java language. The JVM has been improved to allow classes to take advantage of hardware-level concurrency support, and a rich set of new concurrency building blocks has been provided to make it easier to develop concurrent applications.
This tutorial covers the new utility classes for concurrency provided
by JDK 5.0 and demonstrates how these classes offer improved
scalability compared to the existing concurrency primitives
While this tutorial is aimed at a wide range of levels, it is assumed that readers have a basic understanding of threads, concurrency, and the concurrency primitives provided by the Java language, particularly the semantics and correct use of synchronization.
Beginning readers may wish to first consult the "Introduction to Java Threads" tutorial (see Resources), or read the concurrency chapter of a general purpose introductory text on the Java language.
Many of the classes in the
use generics, as
java.util.concurrent has other strong
dependencies on the JDK 5.0 JVM. Users not familiar with generics may
wish to consult resources on the new generics facility in JDK 5.0.
(For those not familiar with generics, you may find it useful to
simply ignore whatever is inside the angle brackets in class and
method signatures in your first pass through this tutorial.)
java.util.concurrent package contains a wealth of
thread-safe, well-tested, high-performance concurrent building blocks.
The goal for the creation of
quite immodestly, to do for concurrency what the Collections framework
did for data structures. By providing a set of reliable,
high-performance concurrency building blocks, developers can improve
the thread safety, scalability, performance, readability, and
reliability of their concurrent classes.
If some of the class names look familiar, it is probably because many
of the concepts in
java.util.concurrent are derived from
library (see Resources).
The improvements for concurrency in JDK 5.0 can be divided into three groups:
JVM-level changes. Most modern processors have some
hardware-level support for concurrency, usually in the form of a
compare-and-swap (CAS) instruction. CAS is a low-level,
fine-grained technique for allowing multiple threads to update a
single memory location while being able to detect and recover from
interference from other threads. It is the basis for many
high-performance concurrent algorithms. Prior to JDK 5.0, the only
primitive in the Java language for coordinating access between
threads was synchronization, which was more heavyweight and
coarse-grained. Exposing CAS makes it possible to develop highly
scalable concurrent Java classes. These changes are intended primarily
for use by JDK library classes, not by developers.
Low-level utility classes -- locking and atomic variables. Using CAS as a concurrency
ReentrantLockclass provides identical locking and memory semantics as the
synchronizedprimitive, while offering better control over locking (such as timed lock waits, lock polling, and interruptible lock waits) and better scalability (higher performance under contention). Most developers will not use the
ReentrantLockclass directly, but instead will use the high-level classes built atop it.
High-level utility classes. These are classes that
implement the concurrency building blocks described in every
computer science text -- semaphores, mutexes, latches, barriers,
exchangers, thread pools, and thread-safe collection classes. Most
developers will be able to use these classes to replace many, if not
all, uses of synchronization,
notify()in their applications, likely to the benefit of performance, readability, and correctness.
This tutorial will focus primarily on the higher-level utility classes
provided by the
java.util.concurrent package -- thread-safe
collections, thread pools, and synchronization utilities. These are
classes that both novices and experts can use "out of the box."
In the first section, we'll review the basics of concurrency, although it should not substitute for an understanding of threads and thread safety. Readers who are not familiar with threading at all should probably first consult an introduction to threads, such as the "Introduction to Java Threads" tutorial (see Resources).
The next several sections explore the high-level utility classes in
java.util.concurrent -- thread-safe collections, thread pools,
semaphores, and synchronizers.
The final sections cover the low-level concurrency building blocks in
java.util.concurrent, and offer some performance
measurements showing the improved scalability of the new
java.util.concurrent package is tightly tied to JDK 5.0; there is
no backport to previous JVM versions. The code examples in this
tutorial will not compile or run on JVMs prior to 5.0, and many of the
code examples use generics, enhanced-for, or other new language
features from JDK 5.0.