Java concurrent Framework之util.concurrent(包括atomic and locks)包概述

来源:互联网 发布:超市利润怎么算法公式 编辑:程序博客网 时间:2024/04/30 06:07

Package java.util.concurrent Description(java.util.concurrent包的描述)

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 java.util.concurrent.locks and java.util.concurrent.atomic packages.
在并发编程中很常用的实用工具类。此包包括了几个小的、已标准化的可扩展框架,以及一些提供有用功能的类,没有这些类,这些功能会很难实现或实现起来冗长乏味。下面简要描述主要的组件。另请参阅locksatomic 包。

Executors(执行程序)

Interfaces. Executor is a simple standardized interface for defining custom thread-like subsystems, includingthread pools,asynchronous IO, andlightweight 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 callingexecute, 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. TheScheduledExecutorService subinterface and associated interfaces add support for delayed and periodic task execution. ExecutorServices provide methods arranging asynchronous execution of any function expressed asCallable, 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. ARunnableFuture is a Future that possesses a run method that upon execution, sets its results.
接口。 Executor 是一个简单的标准化接口,用于定义类似于线程的自定义子系统,包括线程池、异步 IO 和轻量级任务框架。根据所使用的具体 Executor 类的不同,可能在新创建的线程中,现有的任务执行线程中,或者调用execute() 的线程中执行任务,并且可能顺序或并发执行。ExecutorService 提供了多个完整的异步任务执行框架。ExecutorService 管理任务的排队和安排,并允许受控制的关闭。ScheduledExecutorService 子接口及相关的接口添加了对延迟的和定期任务执行的支持。ExecutorService 提供了安排异步执行的方法,可执行由Callable 表示的任何函数,结果类似于RunnableFuture 返回函数的结果,允许确定执行是否完成,并提供取消执行的方法。 RunnableFuture 是拥有 run 方法的 Future, run 方法执行时将设置其结果。

Implementations. Classes ThreadPoolExecutor and ScheduledThreadPoolExecutor provide tunable, flexible thread pools. TheExecutors 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 onExecutors include the concrete classFutureTask providing a common extensible implementation of Futures, andExecutorCompletionService, that assists in coordinating the processing of groups of asynchronous tasks.

实现。ThreadPoolExecutorScheduledThreadPoolExecutor 提供可调的、灵活的线程池。Executors 类提供大多数 Executor 的常见类型和配置的工厂方法,以及使用它们的几种实用工具方法。其他基于 Executor 的实用工具包括具体类 FutureTask,它提供 Future 的常见可扩展实现,以及 ExecutorCompletionService,它有助于协调对异步任务组的处理。

Class ForkJoinPool provides an Executor primarily designed for processing instances ofForkJoinTask and its subclasses. These classes employ a work-stealing scheduler that attains high throughput for tasks conforming to restrictions that often hold in computation-intensive parallel processing.

ForkJoinPool提供执行器主要用于处理ForkJoinTask和它的子类的实例。这些类使用work-stealing调度,实现高吞吐量的限制,往往符合计算密集型的并行处理任务。

Queues(队列)

The ConcurrentLinkedQueue class supplies an efficient scalable thread-safe non-blocking FIFO queue.

Five implementations in java.util.concurrent support the extendedBlockingQueue 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.

Extended interface TransferQueue, and implementation LinkedTransferQueue introduce a synchronous transfer method (along with related features) in which a producer may optionally block awaiting its consumer.

The BlockingDeque interface extends BlockingQueue to support both FIFO and LIFO (stack-based) operations. ClassLinkedBlockingDeque provides an implementation.

java.util.concurrent ConcurrentLinkedQueue 类提供了高效的、可伸缩的、线程安全的非阻塞 FIFO 队列。java.util.concurrent 中的五个实现都支持扩展的BlockingQueue 接口,该接口定义了 put 和 take 的阻塞版本: LinkedBlockingQueueArrayBlockingQueueSynchronousQueuePriorityBlockingQueueDelayQueue。这些不同的类覆盖了生产者-使用者、消息传递、并行任务执行和相关并发设计的大多数常见使用的上下文。

扩展接口TransferQueue和实现LinkedTransferQueue介绍一个同transfer方法一个生产者可以阻塞等待它的消费者。

BlockingDeque 接口扩展BlockingQueue,以支持 FIFO 和 LIFO(基于堆栈)操作。LinkedBlockingDeque 类提供一个实现。

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. All methods that accept timeout parameters treat values less than or equal to zero to mean not to wait at all. To wait "forever", you can use a value of Long.MAX_VALUE.
TimeUnit 类为指定和控制基于超时的操作提供了多重粒度(包括纳秒级)。该包中的大多数类除了包含不确定的等待之外,还包含基于超时的操作。在使用超时的所有情况中,超时指定了在表明已超时前该方法应该等待的最少时间。在超时发生后,实现会“尽力”检测超时。但是,在检测超时与超时之后再次实际执行线程之间可能要经过不确定的时间。接受超时期参数的所有方法将小于等于 0 的值视为根本不会等待。要“永远”等待,可以使用 Long.MAX_VALUE 值。

Synchronizers(同步器)

Five classes aid common special-purpose synchronization idioms.
  • Semaphore is a classic concurrency tool.
  • CountDownLatch is a very simple yet very common utility for blocking until a given number of signals, events, or conditions hold.
  • A CyclicBarrier is a resettable multiway synchronization point useful in some styles of parallel programming.
  • A Phaser provides a more flexible form of barrier that may be used to control phased computation among multiple threads.
  • An Exchanger allows two threads to exchange objects at a rendezvous point, and is useful in several pipeline designs.

五个类可协助实现常见的专用同步语句。 Semaphore 是一个经典的并发工具。 CountDownLatch 是一个极其简单但又极其常用的实用工具,用于在保持给定数目的信号、事件或条件前阻塞执行。 CyclicBarrier 是一个可重置的多路同步点,在某些并行编程风格中很有用。Phaser提供可扩展的barrier形式用于在多线程中控制阶段的计算。 Exchanger 允许两个线程在 collection 点交换对象,它在多流水线设计中是有用的

Concurrent Collections(并发容器)

Besides Queues, this package supplies Collection implementations designed for use in multithreaded contexts:ConcurrentHashMap,ConcurrentSkipListMap,ConcurrentSkipListSet,CopyOnWriteArrayList, and CopyOnWriteArraySet. When many threads are expected to access a given collection, aConcurrentHashMap is normally preferable to a synchronizedHashMap, and aConcurrentSkipListMap is normally preferable to a synchronizedTreeMap. A CopyOnWriteArrayList is preferable to a synchronizedArrayList when the expected number of reads and traversals greatly outnumber the number of updates to a list.

The "Concurrent" prefix used with some classes in this package is a shorthand indicating several differences from similar "synchronized" classes. For examplejava.util.Hashtable andCollections.synchronizedMap(new HashMap()) are synchronized. ButConcurrentHashMap 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 provideweakly 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.

除Queue队列外,此包还提供了设计用于多线程上下文中的 Collection 实现: ConcurrentHashMapConcurrentSkipListMapConcurrentSkipListSetCopyOnWriteArrayListCopyOnWriteArraySet。当期望许多线程访问一个给定 collection 时, ConcurrentHashMap 通常优于同步的HashMapConcurrentSkipListMap 通常优于同步的TreeMap。当期望的读数和遍历远远大于列表的更新数时,CopyOnWriteArrayList 优于同步的ArrayList

此包中与某些类一起使用的“Concurrent”前缀;是一种简写,表明与类似的“同步”类有所不同。例如,java.util.HashtableCollections.synchronizedMap(new HashMap()) 是同步的,但ConcurrentHashMap 则是“并发的”。并发 collection 是线程安全的,但是不受单个排他锁的管理。在 ConcurrentHashMap 这一特定情况下,它可以安全地允许进行任意数目的并发读取,以及数目可调的并发写入。需要通过单个锁不允许对 collection 的所有访问时,“同步”类是很有用的,其代价是较差的可伸缩性。在期望多个线程访问公共 collection 的其他情况中,通常“并发”版本要更好一些。当 collection 是未共享的,或者仅保持其他锁时 collection 是可访问的情况下,非同步 collection 则要更好一些。

大多数并发 Collection 实现(包括大多数 Queue)与常规的 java.util 约定也不同,因为它们的迭代器提供了弱一致的(weakly consistent),而不是快速失败的遍历。弱一致的迭代器是线程安全的(A weakly consistent iterator is thread-safe),但是在迭代时没有必要冻结 collection,所以它不一定反映自迭代器创建以来的所有更新。

concurrent.atomic(并发的原子变量)

原子变量(atomic variable)类用来实现数字和对象引用的原子状态转换。

类的小工具包,支持在单个变量上解除锁的线程安全编程。事实上,此包中的类可将 volatile 值、字段和数组元素的概念扩展到那些也提供原子条件更新操作的类,形式:

  boolean compareAndSet(expectedValue, updateValue);

如果此方法(在不同的类间参数类型也不同)当前保持 expectedValue,则以原子方式将变量设置为 updateValue,并在成功时报告true。此包中的类还包含获取并无条件设置值的方法,以及以下描述的较弱条件的原子更新操作weakCompareAndSet

这些方法的规范使实现能够使用当代处理器上提供的高效机器级别原子指令。但是在某些平台上,该支持可能需要某种形式的内部锁。因而,该方法不能严格保证不被阻塞 - 执行操作之前可能暂时阻塞线程。

AtomicBooleanAtomicIntegerAtomicLongAtomicReference 的实例各自提供对相应类型单个变量的访问和更新。每个类也为该类型提供适当的实用工具方法。例如,类 AtomicLongAtomicInteger 提供了原子增量方法。一个应用程序将按以下方式生成序列号:

class Sequencer {  private final AtomicLong sequenceNumber = new AtomicLong(0);  public long next() {    return sequenceNumber.getAndIncrement();  }}

原子访问和更新的内存效果一般遵循以下可变规则,正如 The Java Language Specification, Third Edition (17.4 Memory Model) 中的声明:

  • get 具有读取 volatile 变量的内存效果。
  • set 具有写入(分配)volatile 变量的内存效果。
  • 除了允许使用后续(但不是以前的)内存操作,其自身不施加带有普通的非 volatile 写入的重新排序约束,lazySet 具有写入(分配)volatile 变量的内存效果。在其他使用上下文中,当为 null 时(为了垃圾回收),lazySet 可以应用不会再次访问的引用。
  • weakCompareAndSet 以原子方式读取和有条件地写入变量但 创建任何 happen-before 排序,因此不提供与除weakCompareAndSet 目标外任何变量以前或后续读取或写入操作有关的任何保证。
  • compareAndSet 和所有其他的读取和更新操作(如 getAndIncrement)都有读取和写入 volatile 变量的内存效果。

除了包含表示单个值的类之外,此包还包含 Updater 类,该类可用于获取任意选定类的任意选定 volatile 字段上的compareAndSet 操作。AtomicReferenceFieldUpdaterAtomicIntegerFieldUpdaterAtomicLongFieldUpdater 是基于反射的实用工具,可以提供对关联字段类型的访问。它们主要用于原子数据结构中,该结构中同一节点(例如,树节点的链接)的几个volatile 字段都独立受原子更新控制。这些类在如何以及何时使用原子更新方面具有更大的灵活性,但相应的弊端是基于映射的设置较为拙笨、使用不太方便,而且在保证方面也较差。

AtomicIntegerArrayAtomicLongArrayAtomicReferenceArray 类进一步扩展了原子操作,对这些类型的数组提供了支持。这些类在为其数组元素提供 volatile 访问语义方面也引人注目,这对于普通数组来说是不受支持的。

原子类也支持 weakCompareAndSet 方法,该方法具有受限制的适用性。在某些平台上,弱版本在正常情况下可能比 compareAndSet 更有效,但不同的是 weakCompareAndSet 方法的任何给定调用可能意外返回 false(即没有明确的原因)。返回 false 仅意味着可以在需要时重新尝试操作,具体取决于重复执行调用的保证,当该变量保持 expectedValue 并且没有其他线程也在尝试设置该变量时,最终将获得成功。(例如,这样的虚假失败可能是由于内存争用的结果,该争用与期望值和当前值是否相等无关)。 此外,weakCompareAndSet 不提供通常需要同步控制的排序保证。但是,在这样的更新与程序的其他 happen-before 排序不相关时,该方法可用于更新计数器和统计数据。当一个线程看到对 weakCompareAndSet 导致的原子变量的更新时,它不一定能看到在 weakCompareAndSet 之前发生的对任何其他变量的更新。例如,在更新性能统计数据时,这也许可以接受,但其他情况几乎不可以。

AtomicMarkableReference 类将单个布尔值与引用关联起来。例如,可以在数据结构内部使用此位,这意味着引用的对象在逻辑上已被删除。AtomicStampedReference 类将整数值与引用关联起来。例如,这可用于表示与更新系列对应的版本号。

设计原子类主要用作各种构造块,用于实现非阻塞数据结构和相关的基础结构类。compareAndSet 方法不是锁的常规替换方法。仅当对象的重要更新限定于单个 变量时才应用它。

原子类不是 java.lang.Integer 和相关类的通用替换方法。它们 定义诸如 hashCodecompareTo 之类的方法。(因为原子变量是可变的,所以对于哈希表键来说,它们不是好的选择。)另外,仅为那些通常在预期应用程序中使用的类型提供类。例如,没有表示byte 的原子类。这种情况不常见,如果要这样做,可以使用AtomicInteger 来保持byte 值,并进行适当的强制转换。也可以使用Float.floatToIntBitsFloat.intBitstoFloat 转换来保持 float 值,使用Double.doubleToLongBitsDouble.longBitsToDouble 转换来保持 double 值。

concurrent.locks(并发的锁)

为锁和等待条件提供一个框架的接口和类,它不同于内置同步和监视器。该框架允许更灵活地使用锁和条件,但以更难用的语法为代价。

Lock 接口支持那些语义不同(重入、公平等)的锁规则,可以在非阻塞式结构的上下文(包括 hand-over-hand 和锁重排算法)中使用这些规则。主要的实现是ReentrantLock

ReadWriteLock 接口以类似方式定义了一些读取者可以共享而写入者独占的锁。此包只提供了一个实现,即ReentrantReadWriteLock,因为它适用于大部分的标准用法上下文。但程序员可以创建自己的、适用于非标准要求的实现。

Condition 接口描述了可能会与锁有关联的条件变量。这些变量在用法上与使用Object.wait 访问的隐式监视器类似,但提供了更强大的功能。需要特别指出的是,单个Lock 可能与多个 Condition 对象关联。为了避免兼容性问题,Condition 方法的名称与对应的Object 版本中的不同。

AbstractQueuedSynchronizer 类是一个非常有用的超类,可用来定义锁以及依赖于排队阻塞线程的其他同步器。AbstractQueuedLongSynchronizer 类提供相同的功能但扩展了对同步状态的 64 位的支持。两者都扩展了类AbstractOwnableSynchronizer(一个帮助记录当前保持独占同步的线程的简单类)。LockSupport 类提供了更低级别的阻塞和解除阻塞支持,这对那些实现自己的定制锁类的开发人员很有用。

Memory Consistency Properties(内存一致性属性)


Chapter 17 of The Java™ Language Specification defines the happens-before relation on memory operations such as reads and writes of shared variables. The results of a write by one thread are guaranteed to be visible to a read by another thread only if the write operation happens-before the read operation. Thesynchronized andvolatile constructs, as well as theThread.start() andThread.join() methods, can formhappens-before relationships. In particular:

  • Each action in a thread happens-before every action in that thread that comes later in the program's order.
  • An unlock (synchronized block or method exit) of a monitor happens-before every subsequent lock (synchronized block or method entry) of that same monitor. And because thehappens-before relation is transitive, all actions of a thread prior to unlockinghappen-before all actions subsequent to any thread locking that monitor.
  • A write to a volatile field happens-before every subsequent read of that same field. Writes and reads ofvolatile fields have similar memory consistency effects as entering and exiting monitors, but donot entail mutual exclusion locking.
  • A call to start on a thread happens-before any action in the started thread.
  • All actions in a thread happen-before any other thread successfully returns from ajoin on that thread.
The methods of all classes in java.util.concurrent and its subpackages extend these guarantees to higher-level synchronization. In particular:
  • Actions in a thread prior to placing an object into any concurrent collectionhappen-before actions subsequent to the access or removal of that element from the collection in another thread.
  • Actions in a thread prior to the submission of a Runnable to an Executor happen-before its execution begins. Similarly for Callables submitted to an ExecutorService.
  • Actions taken by the asynchronous computation represented by a Futurehappen-before actions subsequent to the retrieval of the result viaFuture.get() in another thread.
  • Actions prior to "releasing" synchronizer methods such as Lock.unlock,Semaphore.release, andCountDownLatch.countDownhappen-before actions subsequent to a successful "acquiring" method such asLock.lock,Semaphore.acquire,Condition.await, andCountDownLatch.await on the same synchronizer object in another thread.
  • For each pair of threads that successfully exchange objects via an Exchanger, actions prior to theexchange() in each threadhappen-before those subsequent to the correspondingexchange() in another thread.
  • Actions prior to calling CyclicBarrier.await and Phaser.awaitAdvance (as well as its variants)happen-before actions performed by the barrier action, and actions performed by the barrier actionhappen-before actions subsequent to a successful return from the correspondingawait in other threads.
Java Language Specification 第 17 章定义了内存操作(如共享变量的读写)的happen-before 关系。只有写入操作happen-before 读取操作时,才保证一个线程写入的结果对另一个线程的读取是可视的。synchronizedvolatile 构造 happen-before 关系, Thread.start()Thread.join() 方法形成happen-before 关系。尤其是:
  • 线程中的每个操作 happen-before 稍后按程序顺序传入的该线程中的每个操作。
  • 一个解除锁监视器的(synchronized 阻塞或方法退出)happen-before 相同监视器的每个后续锁(synchronized 阻塞或方法进入)。并且因为happen-before 关系是可传递的,所以解除锁定之前的线程的所有操作happen-before 锁定该监视器的任何线程后续的所有操作。
  • 写入 volatile 字段 happen-before 每个后续读取相同字段。volatile 字段的读取和写入与进入和退出监视器具有相似的内存一致性效果,但 需要互斥锁。
  • 在线程上调用 start happen-before 已启动的线程中的任何线程。
  • 线程中的所有操作 happen-before 从该线程上的 join 成功返回的任何其他线程。
java.util.concurrent 中所有类的方法及其子包扩展了这些对更高级别同步的保证。尤其是:
  • 线程中将一个对象放入任何并发 collection 之前的操作 happen-before 从另一线程中的 collection 访问或移除该元素的后续操作。
  • 线程中向 Executor 提交 Runnable 之前的操作 happen-before 其执行开始。同样适用于向ExecutorService 提交Callables
  • 异步计算(由 Future 表示)所采取的操作 happen-before 通过另一线程中 Future.get() 获取结果后续的操作。
  • “释放”同步储存方法(如 Lock.unlockSemaphore.releaseCountDownLatch.countDown)之前的操作 happen-before 另一线程中相同同步储存对象成功“获取”方法(如Lock.lockSemaphore.acquireCondition.awaitCountDownLatch.await)的后续操作。
  • 对于通过 Exchanger 成功交换对象的每个线程对,每个线程中 exchange() 之前的操作 happen-before 另一线程中对应 exchange() 后续的操作。
  • 调用 CyclicBarrier.await 之前的操作 happen-before 屏障操作所执行的操作,屏障操作所执行的操作happen-before 从另一线程中对应await 成功返回的后续操作。

Java Language Specification 第 17 章 : http://tool.oschina.net/apidocs/apidoc?api=jdk-zh

17. Threads and Locks
17.1. Synchronization
17.2. Wait Sets and Notification
17.2.1. Wait
17.2.2. Notification
17.2.3. Interruptions
17.2.4. Interactions of Waits, Notification, and Interruption
17.3. Sleep and Yield
17.4. Memory Model
17.4.1. Shared Variables
17.4.2. Actions
17.4.3. Programs and Program Order
17.4.4. Synchronization Order
17.4.5. Happens-before Order
17.4.6. Executions
17.4.7. Well-Formed Executions
17.4.8. Executions and Causality Requirements
17.4.9. Observable Behavior and Nonterminating Executions
17.5.final Field Semantics
17.5.1. Semantics offinal Fields
17.5.2. Readingfinal Fields During Construction
17.5.3. Subsequent Modification offinal Fields
17.5.4. Write-protected Fields
17.6. Word Tearing
17.7. Non-atomic Treatment ofdouble andlong

Since:1.5


需要研究一下:Servlet多线程框架,Structs多线程框架,Spring多线程框架,Tomcat多线程框架,Mina/Netty多线程框架

数据库连接池,httpclient连接池,??


参考:

jdk文档:http://tool.oschina.net/apidocs/apidoc?api=jdk_7u4

Java 理论与实践专栏是由经验丰富的 Java 开发人员 Brian Goetz 撰写的一个专栏月刊:http://www.ibm.com/developerworks/cn/linux/l-threading.html

《Java并发编程实践》http://www.javaconcurrencyinpractice.com/

Amino:Amino CBB (Concurrent Building Blocks) 类库将提供优化后的并发线程组件,适用于JDK6.0 及其以后的版本。http://www.oschina.net/p/amino

commons pool:http://commons.apache.org/proper/commons-pool/

 DBCP Component:http://commons.apache.org/proper/commons-dbcp/

Java并发编程框架 Disruptor:http://code.google.com/p/disruptor/

Java 并发处理框架 JPPF网格计算框架 :http://www.jppf.org/

MTRAT:由于并行程序的不确定性造成并行程序的错误很难查找,重现和调试,IBM提供的MTRAT工具 可以收集程序的运行时信息,实时分析程序中所有可能的并行程序错误(如死锁、数据冲突)。http://www.alphaworks.ibm.com/tech/mtrat

深度解析Java 8:JDK1.8 AbstractQueuedSynchronizer的实现分析(上) http://www.infoq.com/cn/articles/jdk1.8-abstractqueuedsynchronizer
深度解析Java 8:AbstractQueuedSynchronizer的实现分析(下) http://www.infoq.com/cn/articles/java8-abstractqueuedsynchronizer


0 0