The Java 2 platform includes a new package of concurrency utilities. These are classes that are designed to be used as building blocks in building concurrent classes or applications. Just as the collections framework simplified the organization and manipulation of in-memory data by providing implementations of commonly used data structures, the concurrency utilities simplify the development of concurrent classes by providing implementations of building blocks commonly used in concurrent designs. The concurrency utilities include a high-performance, flexible thread pool; a framework for asynchronous execution of tasks; a host of collection classes optimized for concurrent access; synchronization utilities such as counting semaphores; atomic variables; locks; and condition variables.
Using the concurrency utilities, instead of developing components such as thread pools yourself, offers a number of advantages:
synchronized
,
volatile
, wait()
, notify()
,
and notifyAll()
) are difficult to use correctly, and
errors using these facilities can be difficult to detect and debug.
By using standardized, extensively tested concurrency building
blocks, many potential sources of threading hazards such as
deadlock, starvation, race conditions, or excessive context
switching are eliminated. The concurrency utilities were
carefully audited for deadlock, starvation, and race
conditions.In short, using the concurrency utilities to implement a concurrent application can help your program be clearer, shorter, faster, more reliable, more scalable, easier to write, easier to read, and easier to maintain.
The concurrency utilities includes:
Executor
interface standardizes invocation, scheduling,
execution, and control of asynchronous tasks according to a set of
execution policies. Implementations are provided that enable tasks
to be executed within the submitting thread, in a
single background thread (as with events in Swing), in a newly
created thread, or in a
thread pool, and developers can create customized
implementations of Executor
that support arbitrary execution policies. The built-in
implementations offer configurable policies such as queue length
limits and saturation
policy that can improve the stability of applications by
preventing runaway resource use.Queue
,
BlockingQueue
and BlockingDeque
interfaces, and high-performance, concurrent implementations of
Map
, List
, and Queue
. See
the Collections Framework Guide for
more information.java.util.concurrent.atomic
package offer higher
performance than would be available by using synchronization (on
most platforms), making them useful for implementing
high-performance concurrent algorithms and conveniently
implementing counters and sequence number generators.java.util.concurrent.locks
package provides a
high-performance lock implementation with the same memory semantics
as synchronization, and it also supports specifying a timeout
when attempting to acquire a lock, multiple condition variables per
lock, nonnested ("hand-over-hand") holding of multiple locks, and
support for interrupting threads that are waiting to acquire a
lock.System.nanoTime
method enables access to a nanosecond-granularity time source for
making relative time measurements and methods that accept
timeouts (such as the
BlockingQueue.offer
,
BlockingQueue.poll
,
Lock.tryLock
,
Condition.await
, and Thread.sleep
)
can take timeout values in nanoseconds. The actual precision of
the System.nanoTime
method is platform-dependent.