java.util.concurrent.CyclicBarrier

public class CyclicBarrier(循环屏障)
extends Object
A synchronization(同步) aid(协助) that allows a set of threads to all wait for each other to reach a common barrier point(常见的障碍点). CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally(偶尔) wait for each other. The barrier is called  cyclic because it can be re-used after the waiting threads are released(释放).

CyclicBarrier supports an optional(可选) Runnable command(命令) that is run once per barrier point(每个障碍点), after the last thread in the party arrives, but before any threads are released. This barrier action is useful for updating shared-state before any of the parties(各方) continue.

Sample usage: Here is an example of using a barrier in a parallel(并行) decomposition(分解) design:


Here, each worker thread processes a row of the matrix then waits at the barrier until all rows have been processed. When all rows are processed the supplied  Runnable barrier action is executed and merges the rows. If the merger determines that a solution has been found then  done() will return  true and each worker will terminate.

If the barrier action does not rely on the parties being suspended when it is executed, then any of the threads in the party could execute that action when it is released. To facilitate this, each invocation of await() returns the arrival index of that thread at the barrier. You can then choose which thread should execute the barrier action, for example:

  if (barrier.await() == 0) {
     // log the completion of this iteration
   }

The CyclicBarrier uses an all-or-none breakage model for failed synchronization attempts: If a thread leaves a barrier point prematurely because of interruption, failure, or timeout, all other threads waiting at that barrier point will also leave abnormally via BrokenBarrierException (or InterruptedException if they too were interrupted at about the same time).

Memory consistency effects: Actions in a thread prior to calling await() happen-before actions that are part of the barrier action, which in turn happen-before actions following a successful return from the correspondingawait() in other threads.

Since:
1.5
See Also:
CountDownLatch

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值