Oracle神谕

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  284 随笔 :: 9 文章 :: 106 评论 :: 0 Trackbacks

Package java.util.concurrent(并发) 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(执行者)
Interfaces. Executor is a simple standardized interface for defining custom thread-like subsystems(子系统), including thread pools(线程池), asynchronous IO(异步IO), and lightweight task frameworks(轻量级任务框架). 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(继续地) or concurrently(并发地). ExecutorService provides a more complete asynchronous(异步的) task execution framework. An ExecutorService manages queuing(队列) and scheduling(行程安排) of tasks, and allows controlled shutdown. The ScheduledExecutorService subinterface adds support for delayed(延时的) 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(可调的), 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(计算调配中心) the processing of groups of asynchronous(异步的) tasks.
[Executor  ExecutorService ScheduledExecutorService ScheduledThreadPoolExecutor ExecutionCompletionService]

Queues(队列)
The java.util.concurrent ConcurrentLinkedQueue class supplies an efficient scalable(可升级的) 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(定时)
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 倒数计秒  latch门插销) is a very simple yet very common utility for blocking until a given number of signals, events, or conditions hold. A CyclicBarrier(cyclic 循环的 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(集合点), and is useful in several pipeline(管道) designs.

Concurrent Collections(并发集合)
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

posted on 2005-10-08 10:43 java世界畅谈 阅读(901) 评论(0)  编辑  收藏 所属分类: JAVA

只有注册用户登录后才能发表评论。


网站导航: