多线程---------------CountDownLatchl

CountDownLatch是通过一个计数器来实现的,计数器的初始值为线程的数量。每当一个线程完成了自己的任务后,计数器的值就会减1。当计数器值到达0时,它表示所有的线程已经完成了任务,然后在闭锁上等待的线程就可以恢复执行任务。
构造器中的计数值(count)实际上就是闭锁需要等待的线程数量。这个值只能被设置一次,而且CountDownLatch没有提供任何机制去重新设置这个计数值。

与CountDownLatch的第一次交互是主线程等待其他线程。主线程必须在启动其他线程后立即调用CountDownLatch.await()方法。这样主线程的操作就会在这个方法上阻塞,直到其他线程完成各自的任务。

其他N个线程必须引用闭锁对象,因为他们需要通知CountDownLatch对象,他们已经完成了各自的任务。这种通知机制是通过 CountDownLatch.countDown()方法来完成的;每调用一次这个方法,在构造函数中初始化的count值就减1。所以当N个线程都调 用了这个方法,count的值等于0,然后主线程就能通过await()方法,恢复执行自己的任务。

package zookeeper.countdownlatch;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public  class CheckStartUp {
    private static List<DangerCenter> dangerCenterList;
    private static CountDownLatch countDownLatch;

    public CheckStartUp() {
    }
    public static  boolean checkAllStation() throws InterruptedException {
        //构造方法中的数字count是线程的数量,每个线程完成后,count-1,当count==0时,唤醒所有等待着的线程
        countDownLatch=new CountDownLatch(3);
        dangerCenterList=new ArrayList<DangerCenter>();
        dangerCenterList.add(new StationBeijing(countDownLatch));
        dangerCenterList.add(new StationJiangsu(countDownLatch));
        dangerCenterList.add(new StationLanKao(countDownLatch));
        //Executors类
        //new FixedThreadPool(num)方法:创建一个固定线程数的线程池 ,多出设置的线程数,则不运行
        Executor executor= Executors.newFixedThreadPool(dangerCenterList.size());
        for(DangerCenter dc:dangerCenterList){
            executor.execute(dc);
        }

        //主线程等待,等待线程执行完毕
        countDownLatch.await();

        for(DangerCenter dc:dangerCenterList){
            if(!dc.isOk()){
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        try {
            boolean flag=CheckStartUp.checkAllStation();
            System.out.println("结果为:"+flag);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
 

package zookeeper.countdownlatch;

import java.util.concurrent.CountDownLatch;

public abstract class DangerCenter implements Runnable {
    private CountDownLatch countDownLatch;//计数器
    private String name;//
    private boolean ok;

    public DangerCenter(CountDownLatch countDownLatch, String name) {
        this.countDownLatch = countDownLatch;
        this.name = name;
        this.ok = false;
    }

    public void run() {
        try {
            check();
            ok=true;
        }catch (Exception e){
            e.printStackTrace();
            ok=false;
        }finally {
            if(countDownLatch!=null){
                countDownLatch.countDown();
            }
        }
    }
    public abstract void check();

    public CountDownLatch getCountDownLatch() {
        return countDownLatch;
    }

    public void setCountDownLatch(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isOk() {
        return ok;
    }

    public void setOk(boolean ok) {
        this.ok = ok;
    }
}

CountDownLatch类闪光词:
1.线程计数器
2.构造函数中的参数为线程运行数
3.countdown()方法有点重要:当线程运行时,上锁,每一个线程完成任务后,初始值-1,到0时,唤醒所有等待中的线程
应用:
在分布式系统中,检查各个中间件是否异常,都返回true时,启动服务。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值