闭锁countDownLatch
闭锁是一种线程同步的方法,作用是:闭锁关闭期间,不允许特定的进程进行执行,在闭锁打开之后,在闭锁范围内的进程都将执行,然后调用await()方法,进行线程阻塞。
通俗的讲就是,一个闭锁相当于一扇大门,在大门打开之前所有线程都被阻断,一旦大门打开所有线程都将通过,但是一旦大门打开,所有线程都通过了,那么这个闭锁的状态就失效了,门的状态也就不能变了,只能是打开状态。也就是说闭锁的状态是一次性的,它确保在闭锁打开之前所有特定的活动都需要在闭锁打开之后才能完成.
CountDownLatch是JDK 5+里面闭锁的一个实现,允许一个或者多个线程等待某个事件的发生。CountDownLatch有一个正数计数器,countDown方法对计数器做减操作,await方法等待计数器达到0。所有await的线程都会阻塞直到计数器为0或者等待线程中断或者超时。
闭锁类:countDownLatch
主要方法为:countDown()、await()
public void countDown() {
sync.releaseShared(1);
}
public void await() throws InterruptedException {
sync.acquireSharedInterruptibly(1);
}
闭锁CountDownLatch
唯一的构造方法CountDownLatch(int count)
,当在闭锁上调用countDown()
方法时,闭锁的计数器将减1,当闭锁计数器为0时,闭锁将打开,所有线程将通过闭锁开始执行。
闭锁的使用:
CountDownLatch downlatch = new CountDownLatch(24) //表示是给这个闭锁内部设定的计算器的值是24
CountDownLatch.countDown() 表示是CountDownLatch调用了countDown()f方法,则闭锁的计算器减1
一下代码参考至http://blog.csdn.net/lmc_wy/article/details/7866863
1. 有五个人,一个裁判。这五个人同时跑,裁判开始计时,五个人都到终点了,裁判喊停,然后统计这五个人从开始跑到最后一个撞线用了多长时间。
- import java.util.concurrent.CountDownLatch;
- public class Race {
- public static void main(String[] args) {
- final int num = 5;
- final CountDownLatch begin = new CountDownLatch(1);
- final CountDownLatch end = new CountDownLatch(num);
- for (int i = 0; i < num; i++) {
- new Thread(new AWorker(i, begin, end)).start();
- }
- // judge prepare...
- try {
- Thread.sleep((long) (Math.random() * 5000));
- } catch (InterruptedException e1) {
- e1.printStackTrace();
- }
- System.out.println("judge say : run !");
- begin.countDown();
- long startTime = System.currentTimeMillis();
- try {
- end.await();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- long endTime = System.currentTimeMillis();
- System.out.println("judge say : all arrived !");
- System.out.println("spend time: " + (endTime - startTime));
- }
- }
- }
- class AWorker implements Runnable {
- final CountDownLatch begin;
- final CountDownLatch end;
- final int id;
- public AWorker(final int id, final CountDownLatch begin,
- final CountDownLatch end) {
- this.id = id;
- this.begin = begin;
- this.end = end;
- }
- @Override
- public void run() {
- try {
- System.out.println(this.id + " ready !");
- begin.await();
- // run...
- Thread.sleep((long) (Math.random() * 10000));
- } catch (Throwable e) {
- e.printStackTrace();
- } finally {
- System.out.println(this.id + " arrived !");
- end.countDown();
- }
- }
- }
2. 继续,还是这五个人(这五个人真无聊..),这次没裁判。规定五个人只要都跑到终点了,大家可以喝啤酒。但是,只要有一个人没到终点,就不能喝。 这里也没有要求大家要同时起跑(当然也可以,加latch)。
- import java.util.concurrent.BrokenBarrierException;
- import java.util.concurrent.CyclicBarrier;
- public class Beer {
- public static void main(String[] args) {
- final int count = 5;
- final CyclicBarrier barrier = new CyclicBarrier(count, new Runnable() {
- @Override
- public void run() {
- System.out.println("drink beer!");
- }
- });
- // they do not have to start at the same time...
- for (int i = 0; i < count; i++) {
- new Thread(new Worker(i, barrier)).start();
- }
- }
- }
- class Worker implements Runnable {
- final int id;
- final CyclicBarrier barrier;
- public Worker(final int id, final CyclicBarrier barrier) {
- this.id = id;
- this.barrier = barrier;
- }
- @Override
- public void run() {
- try {
- System.out.println(this.id + "starts to run !");
- Thread.sleep((long) (Math.random() * 10000));
- System.out.println(this.id + "arrived !");
- this.barrier.await();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (BrokenBarrierException e) {
- e.printStackTrace();
- }
- }
- }