Java多线程编程笔记之CountDownLatch

本篇聊聊同步辅助类CountDownLatch,涉及内容基于JDK7。

1.概述
CountDownLatch允许一个或者多个线程一直等待,直到一组其它操作执行完成。在使用CountDownLatch时,需要指定一个整数值,此值是线程将要等待的操作数。当某个线程为了要执行这些操作而等待时,需要调用await方法。await方法让线程进入休眠状态直到所有等待的操作完成为止。当等待的某个操作执行完成,它使用countDown方法来减少CountDownLatch类的内部计数器。当内部计数器递减为0时,CountDownLatch会唤醒所有调用await方法而休眠的线程们。

2.使用样例
下面代码演示了CountDownLatch简单使用。演示的场景是5位运动员参加跑步比赛,发令枪打响后,5个计时器开始分别计时,直到所有运动员都到达终点。

public class CountDownLatchDemo {
    public static void main(String[] args) {
        Timer timer = new Timer(5);
        new Thread(timer).start();

        for (int athleteNo = 0; athleteNo < 5; athleteNo++) {
            new Thread(new Athlete(timer, "athlete" + athleteNo)).start();
        }
    }
}

class Timer implements Runnable {
    CountDownLatch timerController;

    public Timer(int numOfAthlete) {
        this.timerController = new CountDownLatch(numOfAthlete);
    }

    public void recordResult(String athleteName) {
        System.out.println(athleteName + " has arrived");
        timerController.countDown();
        System.out.println("There are " + timerController.getCount() + " athletes did not reach the end");
    }

    @Override
    public void run() {
        try {
            System.out.println("Start...");
            timerController.await();
            System.out.println("All the athletes have arrived");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Athlete implements Runnable {
    Timer timer;
    String athleteName;

    public Athlete(Timer timer, String athleteName) {
        this.timer = timer;
        this.athleteName = athleteName;
    }

    @Override
    public void run() {
        try {
            System.out.println(athleteName + " start running");
            long duration = (long) (Math.random() * 10);
            Thread.sleep(duration * 1000);
            timer.recordResult(athleteName);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

输出结果如下所示:

Start...
athlete0 start running
athlete1 start running
athlete2 start running
athlete3 start running
athlete4 start running
athlete0 has arrived
There are 4 athletes did not reach the end
athlete3 has arrived
There are 3 athletes did not reach the end
athlete2 has arrived
athlete1 has arrived
There are 1 athletes did not reach the end
There are 2 athletes did not reach the end
athlete4 has arrived
There are 0 athletes did not reach the end
All the athletes have arrived

3.创构造方法
CountDownLatch(int count)构造一个指定计数的CountDownLatch,count为线程将要等待的操作数。

4.常用方法
4.1 await()
调用await方法后,使当前线程在锁存器(内部计数器)倒计数至零之前一直等待,进入休眠状态,除非线程被中断。如果当前计数递减为零,则此方法立即返回,继续执行。

4.2 await(long timeout, TimeUnit unit)
调用await方法后,使当前线程在锁存器(内部计数器)倒计数至零之前一直等待,进入休眠状态,除非线程被 中断或超出了指定的等待时间。如果当前计数为零,则此方法立刻返回true值。

4.3 acountDown()
acountDown方法递减锁存器的计数,如果计数到达零,则释放所有等待的线程。如果当前计数大于零,则将计数减少。如果新的计数为零,出于线程调度目的,将重新启用所有的等待线程。

4.4 getCount()
调用此方法后,返回当前计数,即还未完成的操作数,此方法通常用于调试和测试。

你可能感兴趣的:(Java多线程编程笔记之CountDownLatch)