Java內存Happen-Before

  • 在Thread.start以前的操做,hb 線程裏面的操做
  • 對一個鎖的解鎖,hb 從新得到鎖
  • 一個線程裏的全部操做,hb其餘線程調用這個線程的join返回後。
  • 對一個volatile的寫入操做,hb讀取操做
  • 對一個併發集合(CopyOnWriteList等)的寫入操做,hb於對他的刪除、讀取操做

 

  hb的含義,是以前的操做寫入的東西,對以後的操做都是可見的java

  具備傳遞性,若是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 of volatile 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:app

  • 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 an Executor happen-before its execution begins. Similarly for Callables submitted to an ExecutorService.
  • 在Executor執行以前發生的,老是hb提交的Runnable的代碼
  • Actions taken by the asynchronous computation represented by a Future happen-before actions subsequent to the retrieval of the result via Future.get() in another thread.
  • 在Future裏面執行的,老是hb Future.get以後發生的
  • Actions prior to "releasing" synchronizer methods such as Lock.unlockSemaphore.release, and CountDownLatch.countDown happen-before actions subsequent to a successful "acquiring" method such as Lock.lockSemaphore.acquireCondition.await, and CountDownLatch.await on the same synchronizer object in another thread.
  • 在鎖釋放前發生的(如Lock.unlockSemaphore.release, and CountDownLatch.countDown)老是hb 以後成功獲取鎖後發生的。
  • For each pair of threads that successfully exchange objects via an Exchanger, actions prior to the exchange() in each thread happen-before those subsequent to the corresponding exchange() in another thread.
  • 在兩個經過Exchanger進行交換的線程,在調用exchange以前發生的,老是hb另一個線程響應exchange
  • 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 action happen-before actions subsequent to a successful return from the corresponding await in other threads.
  • 在調用CyclicBarrier.await and Phaser.awaitAdvance以前發生的,老是hb 被內存柵欄執行的動做,內存柵欄執行的動做,老是hb 其餘線程調用await返回以後
相關文章
相關標籤/搜索