CountDownLatch和CyclicBarrier的区别

来源:互联网 发布:网络继续教育 编辑:程序博客网 时间:2024/05/19 20:56
先说两点都知道的:
1.CountDownLatch减计数,CyclicBarrier加计数。
2.CountDownLatch是一次性的,CyclicBarrier可以重用。

然后我们用被大家说烂了的跑步的例子继续说事儿:

1. 有五个人,一个裁判。这五个人同时跑,裁判开始计时,五个人都到终点了,裁判喊停,然后统计这五个人从开始跑到最后一个撞线用了多长时间。

Java代码  收藏代码
  1. import java.util.concurrent.CountDownLatch;  
  2.   
  3. public class Race {  
  4.   
  5.     public static void main(String[] args) {  
  6.         final int num = 5;  
  7.         final CountDownLatch begin = new CountDownLatch(1);  
  8.         final CountDownLatch end = new CountDownLatch(num);  
  9.   
  10.         for (int i = 0; i < num; i++) {  
  11.             new Thread(new AWorker(i, begin, end)).start();  
  12.         }  
  13.   
  14.         // judge prepare...  
  15.         try {  
  16.             Thread.sleep((long) (Math.random() * 5000));  
  17.         } catch (InterruptedException e1) {  
  18.             e1.printStackTrace();  
  19.         }  
  20.   
  21.         System.out.println("judge say : run !");  
  22.         begin.countDown();  
  23.         long startTime = System.currentTimeMillis();  
  24.   
  25.         try {  
  26.             end.await();  
  27.         } catch (InterruptedException e) {  
  28.             e.printStackTrace();  
  29.         } finally {  
  30.             long endTime = System.currentTimeMillis();  
  31.             System.out.println("judge say : all arrived !");  
  32.             System.out.println("spend time: " + (endTime - startTime));  
  33.         }  
  34.   
  35.     }  
  36.   
  37. }  
  38.   
  39. class AWorker implements Runnable {  
  40.     final CountDownLatch begin;  
  41.     final CountDownLatch end;  
  42.     final int id;  
  43.   
  44.     public AWorker(final int id, final CountDownLatch begin,  
  45.             final CountDownLatch end) {  
  46.         this.id = id;  
  47.         this.begin = begin;  
  48.         this.end = end;  
  49.     }  
  50.   
  51.     @Override  
  52.     public void run() {  
  53.         try {  
  54.             System.out.println(this.id + " ready !");  
  55.             begin.await();  
  56.             // run...  
  57.             Thread.sleep((long) (Math.random() * 10000));  
  58.         } catch (Throwable e) {  
  59.             e.printStackTrace();  
  60.         } finally {  
  61.             System.out.println(this.id + " arrived !");  
  62.             end.countDown();  
  63.         }  
  64.     }  
  65.   
  66. }  


CountDownLatch强调的是一个线程(或多个)需要等待另外的n个线程干完某件事情之后才能继续执行。 上述例子,main线程是裁判,5个AWorker是跑步的。运动员先准备,裁判喊跑,运动员才开始跑(这是第一次同步,对应begin)。5个人谁跑到终点了,countdown一下,直到5个人全部到达,裁判喊停(这是第二次同步,对应end),然后算时间。

2. 继续,还是这五个人(这五个人真无聊..),这次没裁判。规定五个人只要都跑到终点了,大家可以喝啤酒。但是,只要有一个人没到终点,就不能喝。 这里也没有要求大家要同时起跑(当然也可以,加latch)。

Java代码  收藏代码
  1. import java.util.concurrent.BrokenBarrierException;  
  2. import java.util.concurrent.CyclicBarrier;  
  3.   
  4. public class Beer {  
  5.   
  6.     public static void main(String[] args) {  
  7.         final int count = 5;  
  8.         final CyclicBarrier barrier = new CyclicBarrier(count, new Runnable() {  
  9.             @Override  
  10.             public void run() {  
  11.                 System.out.println("drink beer!");  
  12.             }  
  13.         });  
  14.   
  15.         // they do not have to start at the same time...  
  16.         for (int i = 0; i < count; i++) {  
  17.             new Thread(new Worker(i, barrier)).start();  
  18.         }  
  19.     }  
  20.   
  21. }  
  22.   
  23. class Worker implements Runnable {  
  24.     final int id;  
  25.     final CyclicBarrier barrier;  
  26.   
  27.     public Worker(final int id, final CyclicBarrier barrier) {  
  28.         this.id = id;  
  29.         this.barrier = barrier;  
  30.     }  
  31.   
  32.     @Override  
  33.     public void run() {  
  34.         try {  
  35.             System.out.println(this.id + "starts to run !");  
  36.             Thread.sleep((long) (Math.random() * 10000));  
  37.             System.out.println(this.id + "arrived !");  
  38.             this.barrier.await();  
  39.         } catch (InterruptedException e) {  
  40.             e.printStackTrace();  
  41.         } catch (BrokenBarrierException e) {  
  42.             e.printStackTrace();  
  43.         }  
  44.     }  
  45. }  


CyclicBarrier强调的是n个线程,大家相互等待,只要有一个没完成,所有人都得等着。正如上例,只有5个人全部跑到终点,大家才能开喝,否则只能全等着。

再强调下,CountDownLatch强调一个线程等多个线程完成某件事情。CyclicBarrier是多个线程互等,等大家都完成。

墨迹完毕...
原创粉丝点击