欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java并发工具类CountDownLatch源码中的例子

程序员文章站 2022-12-30 08:21:59
Java并发工具类CountDownLatch源码中的例子 实例一 原文描述 样本用法:这是一对组中的一个类工作线程使用两个倒计时锁存器: 第一个是启动信号,阻止任何工作人员继续进行直到司机准备好继续进行; 第二个是完成信号,允许驾驶员等待直到所有工人完成。 实例代码 另一种典型用法是将问题分成N个 ......

java并发工具类countdownlatch源码中的例子

实例一

原文描述

/**
 * <p><b>sample usage:</b> here is a pair of classes in which a group
 * of worker threads use two countdown latches:
 * <ul>
 * <li>the first is a start signal that prevents any worker from proceeding
 * until the driver is ready for them to proceed;
 * <li>the second is a completion signal that allows the driver to wait
 * until all workers have completed.
 * </ul>
 **/

样本用法:这是一对组中的一个类工作线程使用两个倒计时锁存器:

第一个是启动信号,阻止任何工作人员继续进行直到司机准备好继续进行;

第二个是完成信号,允许驾驶员等待直到所有工人完成。

实例代码

public class driver {
    public static void main(string[] args) throws interruptedexception {
        countdownlatch startsignal = new countdownlatch(1);
        countdownlatch donesignal = new countdownlatch(5);

        for (int i=0;i<5;++i){
            new thread(new worker(startsignal, donesignal)).start();
        }

        system.out.println("all thread start up.");

        // 任务开始信号
        startsignal.countdown();

        system.out.println(" startsignal count down. ");

        // 等待所有线程执行完成后才执行后续代码
        donesignal.await();

        system.out.println(" all thread end up. ");


        // 执行结果
        // all thread start up.
        // startsignal count down. 
        // hello world!
        // hello world!
        // hello world!
        // hello world!
        // hello world!
        // all thread end up.
    }
}

class worker implements runnable{

    private final countdownlatch startsignal;
    private final countdownlatch donesignal;

    worker(countdownlatch startsignal, countdownlatch donesignal) {
        this.startsignal = startsignal;
        this.donesignal = donesignal;
    }

    @override
    public void run() {
        try {
            // 阻止线程执行,直到startsignal.countdown()后开始执行
            startsignal.await();
            dowork();
            donesignal.countdown();
        } catch (interruptedexception e) {
            e.printstacktrace();
        }

    }

    void dowork(){
        system.out.println("hello world!");
    }
}

实例二

原文描述

/*
 * <p>another typical usage would be to divide a problem into n parts,
 * describe each part with a runnable that executes that portion and
 * counts down on the latch, and queue all the runnables to an
 * executor.  when all sub-parts are complete, the coordinating thread
 * will be able to pass through await. (when threads must repeatedly
 * count down in this way, instead use a {@link cyclicbarrier}.)
 **/

另一种典型用法是将问题分成n个部分,用执行该部分的runnable描述每个部分倒计时锁定,并将所有runnables排队到执行人。

当所有子部件完成时,协调线程将能够通过等待。(当线程必须重复时以这种方式倒数,而不是使用{@link cyclicbarrier})

实例代码

public class driver2 {
    public static void main(string[] args) throws interruptedexception {
        countdownlatch donesignal = new countdownlatch(5);

        // 创建线程池
        executor executor = executors.newsinglethreadexecutor();

        for (int i = 0;i<5;++i){
            // create and start threads
            executor.execute(new workerrunnable(donesignal,i));
        }

        // 等待所有线程任务执行完成
        donesignal.await();

        system.out.println("executor down.");

        // 关闭线程池
        ((executorservice) executor).shutdown();


        // 输出结果
        //  hello world! : 0
        //  hello world! : 1
        //  hello world! : 2
        //  hello world! : 3
        //  hello world! : 4
        //  executor down.
    }
}

class workerrunnable implements runnable {

    private final countdownlatch donesignal;
    private final int i;

    workerrunnable(countdownlatch donesignal, int i) {
        this.donesignal = donesignal;
        this.i = i;
    }

    @override
    public void run() {
        dowork(i);
        donesignal.countdown();
    }

    void dowork(int count) {
        system.out.println("hello world! : " + count);
    }
}