跳至主要內容

java.util.concurrent 简介

lament-z大约 4 分钟

java.util.concurrent 简介

平时说 JUC ,通常默认为以下三个包的统称: java.util.concurrentjava.util.concurrent.atomicjava.util.concurrent.locks

大体可以分为以下几个部分:

  • locks 包含 AQS, LockSupport ,以及基于 AQS 的锁实现:ReentrantLock,ReentrantReadWriteLock 等。

  • 原子类 atomic
    一堆原子类组成的包,可以看作是工具包,主要用来支持对变量进行非阻塞的线程安全编程。

  • 同步辅助类

CountDownLatch(赛车),CyclicBarrier(厨房),Phaser(前面二者的加强版),Semaphore(信号量),Exchanger(这个是线程之间交换变量值的)

  • 线程执行和管理

Future :代表异步计算的返回结果。

Executor, ExecutorService :线程任务的执行,执行各种已提交的 Runnable 任务. (Note: 线程的设计把执行机制和执行内容分开,各种 Runnable,Callable 任务就是执行内容,而执行机制,也就是具体如何执行 task 交给 Executor)。

ThreadPoolExecutor: 线程池版Executor,它的基本逻辑是从线程池里获取线程,然后执行。

Executers: 工具类,他是 Executor、ExecutorService、ScheduledExecutorService、ThreadFactory 和 Callable 类的工厂和工具方法的合集。

  • 并发集合 ArrayBlockingQueue,ConcurrentHashMap,ConcurrentLinkedDeque,ConcurrentLinkedQueue,ConcurrentSkipListMap,ConcurrentSkipListSet

CopyOnWriteArrayList CopyOnWriteArraySet

LinkedBlockingDeque LinkedBlockingQueue LinkedTransferQueue

PriorityBlockingQueue 等等

文档翻译

java.util.concurrent,简称JUC,是 Java 为并发编程提供的标准化可扩展的框架。

主要组件包括以下部分:Executor、Queues、Timing、Synchronizers、concurrent Collections、Memory Consistency Properties。

Memory Consistency Properties

内存一致性属性,文档中这部分实际上就是介绍了一下 juc 包中如何实践 happen-before 原则和一些例子。 比如“线程必须事先将元素放入并发集合类中”这个动作happen-before于“线程从并发集合类中访问/删除这个元素”。 详见文档,如果还是对 happen-before 不理解,请重新阅读之前关于 happen-before 以及 偏序关系的介绍。

concurrent Collections

并发集合类,就是用于并发环境下的集合类,比如 ConcurrentHashMap 可以看成是同步版本的 HashMapConcurrentSkipListMap 同步版本的 TreeMap等等。

包中的很多类都带有 concurrent 前缀,它的含义不仅仅是线程安全,还可以“并发”访问。

比如我们可以通过Collections.synchronizedMap(new HashMap())来保证 hashMap 线程安全,但它不是 concurrent 的,因为这里 hashMap 只是通过排它锁,以阻塞同步的方式来保证了线程安全,本质上同一时间只有一个线程访问它并没有并发concurrent。 而并发集合类,比如ConcurrentHashMap则是在特定情况下可以允许任意数量线程并发读取 or 可控数量线程并发写入,同时还保证线程安全。

所以阅读这部分源码or文档时需要注意 synchronizedconcurrent 的区别,Collections.synchronizedMap(new HashMap())是同步的(synchronized),ConcurrentHashMap 是并发的(concurrent)。

Synchronizers

主要是五个常见场景的同步器:Semaphore、CountDownLatch、CyclicBarrier、Phaser、Exchanger。

Timing

主要就是 TimeUnit 类,提供了基于不同时间粒度的操作方法,比如 sleep wait join 啥的。同时附带了一堆时间转换的方法以及时间单位的定义。

Queues

这部分提供了阻塞/非阻塞的各种线程安全队列。 详情看文档。

Executor

Executor 是个非常简单的标准化接口,只有一个方法 void execute(Runnable command);,它可以用来定义线程池、异步IO、轻量级任务框架这类型的线程类子系统。

一个特定/具体的 Executor 实现可以是正在创建 Thread,或者是一个正在执行中的 Executor ,或者是 一个线程调用 execute()方法(异步 or 顺序执行)。

可以通过 JUC 包内的例子来理解上面的定义。

以异步任务执行框架为例,ExecutorService (interface ExecutorService extends Executor)在 Executor 基础上进行扩展,通过提供线程状态方法、任务的提交关闭调用等方法,定义了一个异步任务执行框架。

更进一步 ScheduledExecutorService 接口,它通过继承 ExecutorService 接口进一步扩展出了延迟任务和周期任务的执行。

具体实现

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.

reference

oracle java8 docopen in new window

上次编辑于:
贡献者: Lament