zoukankan      html  css  js  c++  java
  • 闭锁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. 有五个人,一个裁判。这五个人同时跑,裁判开始计时,五个人都到终点了,裁判喊停,然后统计这五个人从开始跑到最后一个撞线用了多长时间。

    [java] view plaincopy
     
    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. }    


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

    [java] view plaincopy
     
      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. }  
  • 相关阅读:
    【设计模式】六大原则
    【HTML5】表单属性
    【HTML5】表单元素
    【HTML5】input类型
    【HTML5】Server-Sent服务器发送事件
    【HTML5】Web Workers
    【HTML5】Application Cache应用程序缓存
    【HTML5】Web存储
    【HTML5】地理定位
    【HTML5】Canvas和SVG的区别
  • 原文地址:https://www.cnblogs.com/yongxingg/p/3671060.html
Copyright © 2011-2022 走看看