Package java.util.concurrent(并發(fā)) Description
Utility classes commonly useful in concurrent programming. This package includes a few small standardized extensible frameworks, as well as some classes that provide useful functionality and are otherwise tedious(沉悶的) or difficult to implement. Here are brief(摘要的) descriptions of the main components. See also the locks(鎖) and atomic(原子的) packages.
Executors(執(zhí)行者)
Interfaces. Executor is a simple standardized interface for defining custom thread-like subsystems(子系統(tǒng)), including thread pools(線程池), asynchronous IO(異步IO), and lightweight task frameworks(輕量級(jí)任務(wù)框架). Depending on which concrete Executor class is being used, tasks may execute in a newly(以新的方式) created thread, an existing task-execution thread, or the thread calling execute(), and may execute sequentially(繼續(xù)地) or concurrently(并發(fā)地). ExecutorService provides a more complete asynchronous(異步的) task execution framework. An ExecutorService manages queuing(隊(duì)列) and scheduling(行程安排) of tasks, and allows controlled shutdown. The ScheduledExecutorService subinterface adds support for delayed(延時(shí)的) and periodic(定期的) task execution. ExecutorServices provide methods arranging(安排的) asynchronous execution of any function expressed as Callable, the result-bearing analog(類似物) of Runnable. A Future returns the results of a function, allows determination of whether execution has completed, and provides a means to cancel execution.
Implementations. Classes ThreadPoolExecutor and ScheduledThreadPoolExecutor provide tunable(可調(diào)的), flexible(靈活的) thread pools. The Executors class provides factory methods for the most common kinds and configurations of Executors, as well as a few utility methods for using them. Other utilities based on Executors include the concrete class FutureTask providing a common extensible implementation of Futures, and ExecutorCompletionService, that assists in coordinating(計(jì)算調(diào)配中心) the processing of groups of asynchronous(異步的) tasks.
[Executor ExecutorService ScheduledExecutorService ScheduledThreadPoolExecutor ExecutionCompletionService]
Queues(隊(duì)列)
The java.util.concurrent ConcurrentLinkedQueue class supplies an efficient scalable(可升級(jí)的) thread-safe(線程安全) non-blocking(非模塊化) FIFO queue. Five implementations in java.util.concurrent support the extended BlockingQueue interface, that defines blocking versions of put and take: LinkedBlockingQueue, ArrayBlockingQueue, SynchronousQueue, PriorityBlockingQueue, and DelayQueue. The different classes cover the most common usage contexts for producer-consumer, messaging, parallel tasking, and related concurrent designs.
[LinkedBlockingQueue ArrayBlockingQueue SynchronousQueue PriorityBlockingQueue DelayQueue]
Timing(定時(shí))
The TimeUnit class provides multiple granularities<間隔尺寸> (including nanoseconds十億分之一秒) for specifying and controlling time-out(暫停) based operations. Most classes in the package contain operations based on time-outs in addition to(除...以外) indefinite(不確定的) waits. In all cases that time-outs are used, the time-out specifies the minimum time that the method should wait before indicating(指出) that it timed-out. Implementations make a "best effort" to detect(察覺) time-outs as soon as possible after they occur. However, an indefinite(模糊的) amount of time may elapse(流逝) between a time-out being detected and a thread actually executing again after that time-out.
Synchronizers(同步)
Four classes aid(幫助) common special-purpose synchronization idioms(語法). Semaphore(旗語) is a classic concurrency tool. CountDownLatch(Countdown 倒數(shù)計(jì)秒 latch門插銷) is a very simple yet very common utility for blocking until a given number of signals, events, or conditions hold. A CyclicBarrier(cyclic 循環(huán)的 barrier 屏障) is a resettable(可重置的) multiway(多路的) synchronization point useful in some styles of parallel programming. An Exchanger allows two threads to exchange(交換) objects at a rendezvous point(集合點(diǎn)), and is useful in several pipeline(管道) designs.
Concurrent Collections(并發(fā)集合)
Besides Queues, this package supplies a few Collection implementations designed for use in multithreaded contexts: ConcurrentHashMap, CopyOnWriteArrayList, and CopyOnWriteArraySet.
The "Concurrent" prefix used with some classes in this package is a shorthand(速記) indicating several differences from similar "synchronized" classes. For example java.util.Hashtable and Collections.synchronizedMap(new HashMap()) are synchronized. But ConcurrentHashMap is "concurrent". A concurrent collection is thread-safe, but not governed by a single exclusion lock. In the particular case of ConcurrentHashMap, it safely permits any number of concurrent reads as well as a tunable number of concurrent writes. "Synchronized" classes can be useful when you need to prevent all access to a collection via a single lock, at the expense of poorer scalability. In other cases in which multiple threads are expected to access a common collection, "concurrent" versions are normally preferable. And unsynchronized collections are preferable when either collections are unshared, or are accessible only when holding other locks.
Most concurrent Collection implementations (including most Queues) also differ from the usual java.util conventions in that their Iterators provide weakly consistent rather than fast-fail traversal. A weakly consistent iterator is thread-safe, but does not necessarily freeze the collection while iterating, so it may (or may not) reflect any updates since the iterator was created.
Since:
1.5