A thread that calls the await method will block until the latch has counted down to zero.
CyclicBarrier A utility that forces all threads using the barrier to wait until all those threads are
ready to pass the barrier. This is useful for multi-phase algorithms in which all threads must complete
a given phase before any threads commence the next phase.
•
Exchanger A simple utility that allows two threads to exchange data. The first thread to arrive at
the exchanger waits until the other arrives, they then exchange data and proceed.
•
An Executor knows how to execute a taskwhich is represented by a Runnable objectand replaces the
direct creation of new threads within an application. Depending on the implementation used, the Executor
may execute the task in the current thread, submit the task to a thread pool for later execution, or perhaps
create a new thread to execute the task. The Executor abstracts all the details of thread creation and
management and allows the problem of executing tasks to be separated from the problem of generating tasks.
An ExecutorService expands on the basic executor to provide lifecycle managementsuch as shutting
down an executor. Particular implementations of ExecutorService provide thread pools
(ThreadPoolExecutor) and permit the scheduling of tasks in the future
(ScheduledThreadPoolExecutor).
The Callable interface defines a task that can return a result (unlike a Runnable). A Future represents
a computationsuch as that in a Callablethat may not yet have completed or even begun. You can submit a
Future to an Executor (using the concrete FutureTask class) knowing that at some time in the future
the result of the computation will be available. You can use the Future to ask whether the task has
completed or to block until the task completes and the result is available.
The java.util.concurrent.locks subpackage defines the Lock and Condition interfaces and the
ReentrantLock implementation. Lock abstracts the functionality of locking, as implied in the use of
synchronized blocks and methods, to allow more flexible locking. A Lock can be locked in one method
and unlocked in a different onesomething that is needed in some concurrent traversal algorithms, and which is
impossible to do with synchronized blocks or methods. In addition, the methods of Lock allow you to
acquire the lock only if it is availablethat is, you don't block trying to acquire the lockor to stop trying to
acquire the lock if interrupted or if a certain time has elapsed.
A Condition abstracts the functionality of the Object monitor methods wait, notify, and
notifyAll into a distinct interface with methods await, signal, and signalAll. Condition
objects are used with Lock objects the same way that wait/notify are used with monitors. The advantage
is that while there is only a single wait-set per object, a Lock can have as many associated Condition
objects as you need. The ReentrantLock class is one useful implementation of Lock that gives you
Condition objects that are functionally equivalent to synchronized methods and blocks, plus the use of
the monitor methods.
The package also contains the ReadWriteLock interfaceand an implementationfor a lock that allows for
both exclusive use and shared useinformally, only one writer may hold the lock (exclusive) but arbitrarily
many readers can hold the lock (shared).
The java.util.concurrent.atomic package provides classes that represents values (int, long,
references, and arrays of these values) that can be manipulated atomically. These classes provide operations
that without the use of locks or in-built synchronization can, for example, get, set, increment, or decrement an
integer. Atomic variables are used in the implementation of high-performance concurrent algorithms. For each
basic atomic variable class, such as AtomicInteger, there is a corresponding atomic field updater class,
such as AtomicIntegerFieldUpdater, that allows you to access a given field of an object atomically.
These classes also provide the compareAndSet operation that is the fundamental operation in most
lock-free and wait-free concurrent algorithms. It will atomically set a variable to a new value only if the
variable currently holds an expected value.