zoukankan      html  css  js  c++  java
  • CountDownLatch简单使用

    CountDownLatch介绍

     CountDownLatch是JAVA提供在java.util.concurrent包下的一个辅助类,可以把它看成是一个计数器,其内部维护着一个count计数,只不过对这个计数器的操作都是原子操作,同时只能有一个线程去操作这个计数器,CountDownLatch通过构造函数传入一个初始计数值,调用者可以通过调用CounDownLatch对象的cutDown()方法,来使计数减1;如果调用对象上的await()方法,那么调用者就会一直阻塞在这里,直到别人通过cutDown方法,将计数减到0,才可以继续执行。

      使用场景

    有时候会有这样的需求,多个线程同时工作,然后其中几个可以随意并发执行,但有一个线程需要等其他线程工作结束后,才能开始。举个例子,开启多个线程分块下载一个大文件,每个线程只下载固定的一截,最后由另外一个线程来拼接所有的分段,那么这时候我们可以考虑使用CountDownLatch来控制并发。

    示例

    [java] view plain copy
     
    1. package org.test.other;  
    2.   
    3. import java.util.concurrent.CountDownLatch;  
    4.   
    5. public class Sample {  
    6.     /** 
    7.      * 计数器,用来控制线程 
    8.      * 传入参数2,表示计数器计数为2 
    9.      */  
    10.     private final static CountDownLatch mCountDownLatch = new CountDownLatch(5);  
    11.   
    12.     /** 
    13.      * 示例工作线程类 
    14.      */  
    15.     private static class WorkingThread extends Thread {  
    16.         private final String mThreadName;  
    17.         private final int mSleepTime;  
    18.         public WorkingThread(String name, int sleepTime) {  
    19.             mThreadName = name;  
    20.             mSleepTime = sleepTime;  
    21.         }  
    22.           
    23.         @Override  
    24.         public void run() {  
    25.             System.out.println("[" + mThreadName + "] started!");  
    26.             try {    
    27.                     Thread.sleep(mSleepTime);    
    28.             } catch (InterruptedException e) {    
    29.                     e.printStackTrace();    
    30.             }  
    31.             System.out.println("[" + mThreadName + "] end!");   
    32.             mCountDownLatch.countDown();  
    33.         }  
    34.     }  
    35.       
    36.     /** 
    37.      * 示例线程类 
    38.      */  
    39.     private static class SampleThread extends Thread {  
    40.           
    41.         @Override  
    42.         public void run() {  
    43.             System.out.println("[SampleThread] started!");  
    44.             try {  
    45.                 // 会阻塞在这里等待 mCountDownLatch 里的count变为0;  
    46.                 // 也就是等待另外的WorkingThread调用countDown()  
    47.                 mCountDownLatch.await();  
    48.             } catch (InterruptedException e) {  
    49.                   
    50.             }  
    51.             System.out.println("[SampleThread] end!");  
    52.         }  
    53.     }  
    54.       
    55.     public static void main(String[] args) throws Exception {  
    56.         // 最先run SampleThread  
    57.         new SampleThread().start();  
    58.         // 运行两个工作线程  
    59.         // 工作线程1运行5秒  
    60.         new WorkingThread("WorkingThread1", 5000).start();  
    61.         // 工作线程2运行5秒  
    62.         new WorkingThread("WorkingThread2", 5000).start();  
    63.         new WorkingThread("WorkingThread3", 5000).start();  
    64.         new WorkingThread("WorkingThread4", 5000).start();  
    65.         new WorkingThread("WorkingThread5", 5000).start();  
    66.     }  
    67. }  
  • 相关阅读:
    Algs4-1.4DoublingRatio
    Algs4-1.4TwoSumFast
    Algs4-1.4ThreeSumFast
    Algs4-1.4ThreeSum
    Algs4-1.4TwoSum
    Algs4-1.3.50快速出错的迭代器
    *Algs4-1.3.49栈与队列-(未解决)
    Algs4-1.3.4X栈与队列-两个栈实现一个队列均摊O(1)
    Algs4-1.3.47可连接的队列、栈或steque
    Java的垃圾回机机制(见过讲得最清楚的)
  • 原文地址:https://www.cnblogs.com/cxxjohnson/p/9056684.html
Copyright © 2011-2022 走看看