CountDownLatch的介绍和使用

1、类介绍
java.util.concurrent
类 CountDownLatch
java.lang.Object
java.util.concurrent.CountDownLatch

2、CountDownLatch作用
个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。意思就是说,有一个工厂老板,可以安排
CountDownLatch去监视所有工人工作,等到所有工人都做完了,工厂老板才会可以离开

2、CountDownLatch使用代码
 /**
     * Announce all seeds from configured source to SeedListeners 
     * (including nonseed lines mixed in). 
     * @see org.archive.modules.seeds.SeedModule#announceSeeds()
     */
    public void announceSeeds() {
        if(getBlockAwaitingSeedLines()>-1) { //如果不是需要全部导入种子
            final CountDownLatch latch = new CountDownLatch(getBlockAwaitingSeedLines());
            new Thread(){
                @Override
                public void run() {
                    announceSeeds(latch); 
                    while(latch.getCount()>0) {
                        latch.countDown();
                    }
                }
            }.start();
            try {
                latch.await();
            } catch (InterruptedException e) {
                // do nothing
            } 
        } else {
            announceSeeds(null); 
        }
    }

上面代码的意思是说:

 /**
     * Announce all seeds (and nonseed possible-directive lines) from
     * the given Reader
     * @param reader source of seed/directive lines
     * @param latchOrNull if non-null, sent countDown after each line, allowing 
     * another thread to proceed after a configurable number of lines processed
     */
    protected void announceSeedsFromReader(BufferedReader reader, CountDownLatch latchOrNull) {
        String s;
        /**
         * 获取迭代器,什么的迭代器呢?文本文件一行一行读取的迭代器,这个迭代器考虑了注释以#号开始
         * 还考虑了空白行,使用正则表达式去处理的,^\\s*[\\S]+\\s*(#.*)?$
         * 注释行:\\s*(#.*)?
         * */
        Iterator<String> iter = 
            new RegexLineIterator(
                    new LineReadingIterator(reader),
                    RegexLineIterator.COMMENT_LINE,
                    RegexLineIterator.NONWHITESPACE_ENTRY_TRAILING_COMMENT,
                    RegexLineIterator.ENTRY);

        int count = 0; 
        while (iter.hasNext()) {
            s = (String) iter.next();
            if(Character.isLetterOrDigit(s.charAt(0))) {
                // consider a likely URI
                seedLine(s);
                count++;
                if(count%20000==0) {
                    System.runFinalization();
                }
            } else {
                // report just in case it's a useful directive
                nonseedLine(s);
            }
            if(latchOrNull!=null) {
                latchOrNull.countDown(); 
            }
        }
        publishConcludedSeedBatch(); 
    }
    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,CountDownLatch是一个同步工具类,用于控制多个线程的并发执行。它的主要作用是让一个或多个线程等待其他线程的完成。CountDownLatch的计数器初始值为正整数,每次调用countDown()方法会将计数器减1,计数器为0时,等待线程开始执行。 在Java中使用CountDownLatch的步骤如下: 1. 创建一个CountDownLatch对象,将计数器初始值设定为需要等待的线程数。 2. 在需要等待的线程中,调用countDown()方法,每次调用都会将计数器减1。 #### 引用[.reference_title] - *1* [Java中CountDownLatch用法解析](https://download.csdn.net/download/weixin_38543950/12788169)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [java并发之CountdownLatch](https://blog.csdn.net/qq_48508278/article/details/124265478)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [CountDownLatch介绍使用【Java多线程必备】](https://blog.csdn.net/qq_45871274/article/details/130223673)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值