- 在Thread.start之前的操作,hb 线程里面的操作
- 对一个锁的解锁,hb 重新获得锁
- 一个线程里的所有操作,hb其他线程调用这个线程的join返回后。
- 对一个volatile的写入操作,hb读取操作
- 对一个并发集合(CopyOnWriteList等)的写入操作,hb于对他的删除、读取操作
hb的含义,是之前的操作写入的东西,对之后的操作都是可见的
具有传递性,如果A hb B,B hb C,那么A hb C
- Each action in a thread happens-before every action in that thread that comes later in the program's order.
- 在同一个线程里面,前面的操作总是 hb 后面的操作
- 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 the happens-before relation is transitive, all actions of a thread prior to unlocking happen-before all actions subsequent to any thread locking that monitor. - 对于同一个锁,释放锁之前的操作,总是 hb 之后获取锁的操作。因此,之前获取锁的线程,总是hb之后获取锁的线程。
- 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 do not entail mutual exclusion locking. - 对Volatile的写操作,总是hb读操作。对volatile的读写在进入和退出锁上有相似的的内存一致性,但不是互斥锁。
- A call to
start
on a thread happens-before any action in the started thread. - 在Thread.start之前发生的,总是hb 线程里面执行的
- All actions in a thread happen-before any other thread successfully returns from a
join
on that thread. - 在线程里面发生的,总是hb 线程Join返回之后发生的
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 collection happen-before actions subsequent to the access or removal of that element from the collection in another thread.
- 在同步集合中之前放入对象总是hb之后的读取和删除
- Actions in a thread prior to the submission of a
Runnable
to anExecutor
happen-before its execution begins. Similarly forCallables
submitted to anExecutorService
. - 在Executor执行之前发生的,总是hb提交的Runnable的代码
- Actions taken by the asynchronous computation represented by a
Future
happen-before actions subsequent to the retrieval of the result viaFuture.get()
in another thread. - 在Future里面执行的,总是hb Future.get之后发生的
- Actions prior to "releasing" synchronizer methods such as
Lock.unlock
,Semaphore.release
, andCountDownLatch.countDown
happen-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. - 在锁释放前发生的(如
Lock.unlock
,Semaphore.release
, andCountDownLatch.countDown)总是hb 之后成功获取锁后发生的。
- For each pair of threads that successfully exchange objects via an
Exchanger
, actions prior to theexchange()
in each thread happen-before those subsequent to the correspondingexchange()
in another thread. - 在两个通过Exchanger进行交换的线程,在调用exchange之前发生的,总是hb另外一个线程响应exchange
- Actions prior to calling
CyclicBarrier.await
andPhaser.awaitAdvance
(as well as its variants) happen-before actions performed by the barrier action, and actions performed by the barrier action happen-before actions subsequent to a successful return from the correspondingawait
in other threads. - 在调用
CyclicBarrier.await
andPhaser.awaitAdvance之前发生的,总是hb 被内存栅栏执行的动作,内存栅栏执行的动作,总是hb 其他线程调用await返回之后