CyclicBarrier
2016-11-01 23:47
190 查看
The
reach it, before any of the threads can continue. Here is a diagram illustrating that:
The threads wait for each other by calling the
released and can continue running.
When you create a
Here is how a thread waits at a
You can also specify a timeout for the waiting thread. When the timeout has passed the thread is also released, even if not all N threads are waiting at the
The waiting threads waits at the
The last thread arrives (calls await() )
The thread is interrupted by another thread (another thread calls its interrupt() method)
Another waiting thread is interrupted
Another waiting thread times out while waiting at the
The
The
to the
Here is a code example that shows you how to use a
Here is the
Here is the console output for an execution of the above code. Note that the sequence in which the threads gets to write to the console may vary from execution to execution. Sometimes
first etc.
java.util.concurrent.CyclicBarrierclass is a synchronization mechanism that can synchronize threads progressing through some algorithm. In other words, it is a barrier that all threads must wait at, until all threads
reach it, before any of the threads can continue. Here is a diagram illustrating that:
Two threads waiting for each other at CyclicBarriers. |
await()method on the
CyclicBarrier. Once N threads are waiting at the
CyclicBarrier, all threads are
released and can continue running.
Creating a CyclicBarrier
When you create a CyclicBarrieryou specify how many threads are to wait at it, before releasing them. Here is how you create a
CyclicBarrier:
CyclicBarrier barrier = new CyclicBarrier(2);
Waiting at a CyclicBarrier
Here is how a thread waits at a CyclicBarrier:
barrier.await();
You can also specify a timeout for the waiting thread. When the timeout has passed the thread is also released, even if not all N threads are waiting at the
CyclicBarrier. Here is how you specify a timeout:
barrier.await(10, TimeUnit.SECONDS);
The waiting threads waits at the
CyclicBarrieruntil either:
The last thread arrives (calls await() )
The thread is interrupted by another thread (another thread calls its interrupt() method)
Another waiting thread is interrupted
Another waiting thread times out while waiting at the
CyclicBarrier
The
CyclicBarrier.reset()method is called by some external thread.
CyclicBarrier Action
The CyclicBarriersupports a barrier action, which is a
Runnablethat is executed once the last thread arrives. You pass the
Runnablebarrier action
to the
CyclicBarrierin its constructor, like this:
Runnable barrierAction = ... ; CyclicBarrier barrier = new CyclicBarrier(2, barrierAction);
CyclicBarrier Example
Here is a code example that shows you how to use a CyclicBarrier:
Runnable barrier1Action = new Runnable() { public void run() { System.out.println("BarrierAction 1 executed "); } }; Runnable barrier2Action = new Runnable() { public void run() { System.out.println("BarrierAction 2 executed "); } }; CyclicBarrier barrier1 = new CyclicBarrier(2, barrier1Action); CyclicBarrier barrier2 = new CyclicBarrier(2, barrier2Action); CyclicBarrierRunnable barrierRunnable1 = new CyclicBarrierRunnable(barrier1, barrier2); CyclicBarrierRunnable barrierRunnable2 = new CyclicBarrierRunnable(barrier1, barrier2); new Thread(barrierRunnable1).start(); new Thread(barrierRunnable2).start();
Here is the
CyclicBarrierRunnableclass:
public class CyclicBarrierRunnable implements Runnable{ CyclicBarrier barrier1 = null; CyclicBarrier barrier2 = null; public CyclicBarrierRunnable( CyclicBarrier barrier1, CyclicBarrier barrier2) { this.barrier1 = barrier1; this.barrier2 = barrier2; } public void run() { try { Thread.sleep(1000); System.out.println(Thread.currentThread().getName() + " waiting at barrier 1"); this.barrier1.await(); Thread.sleep(1000); System.out.println(Thread.currentThread().getName() + " waiting at barrier 2"); this.barrier2.await(); System.out.println(Thread.currentThread().getName() + " done!"); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } } }
Here is the console output for an execution of the above code. Note that the sequence in which the threads gets to write to the console may vary from execution to execution. Sometimes
Thread-0prints first, sometimes
Thread-1prints
first etc.
Thread-0 waiting at barrier 1 Thread-1 waiting at barrier 1 BarrierAction 1 executed Thread-1 waiting at barrier 2 Thread-0 waiting at barrier 2 BarrierAction 2 executed Thread-0 done! Thread-1 done!
相关文章推荐
- 同步工具类之 CyclicBarrier
- CyclicBarrier介绍
- JAVA并发编程之 CyclicBarrier
- Java并发工具类CountDownLatch和CyclicBarrier
- CyclicBarrier 栅栏
- 线程通信——CountDownLatch和CyclicBarrier
- 【Java并发】(二) 线程同步之Thread.join()、CountDownLatch、CyclicBarrier
- 倒计数器:CountDownLatch | 循环栅栏:CyclicBarrier
- Java多线程编程-(6)-两种常用的线程计数器CountDownLatch和循环屏障CyclicBarrier
- CyclicBarrier 简单的demo,思考应用场景
- CyclicBarrier的使用
- 并发编程之屏障CyclicBarrier
- CyclicBarrier分析与实例
- CountDownLatch与CyclicBarrier
- Java 并发专题 : CyclicBarrier 打造一个安全的门禁系统
- CyclicBarrier的使用
- 篱栅:CyclicBarrier
- 线程辅助类(四)--CyclicBarrier
- CyclicBarrier
- Java多线程编程-(8)-两种常用的线程计数器CountDownLatch和循环屏障CyclicBarrier