CountDownLatch与Callable实现线程结束开关控制

ApplicationStartupUtil

package com.chinaso.mytest;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import com.chinaso.mytest.CacheHealthChecker;
import com.chinaso.mytest.DatabaseHealthChecker;
import com.chinaso.mytest.NetworkHealthChecker;


public class ApplicationStartupUtil{
    //List of service checkers
    private static List<Callable<Boolean>> _services;   
 
    //This latch will be used to wait on
    private static CountDownLatch _latch;
 
    private ApplicationStartupUtil(){
    }
 
    private final static ApplicationStartupUtil INSTANCE = new ApplicationStartupUtil();
 
    public static ApplicationStartupUtil getInstance(){
        return INSTANCE;
    }
 
    public static boolean checkExternalServices() throws Exception{
        //Initialize the latch with number of service checkers
        _latch = new CountDownLatch(3);                   //计数器初始化为 3 
 
        //All add checker in lists
        _services = new ArrayList<Callable<Boolean>>();
        _services.add(new NetworkHealthChecker(_latch));  //把三个线程加入list,还能遍历list来获得线程的成员变量值?
        _services.add(new CacheHealthChecker(_latch));
        _services.add(new DatabaseHealthChecker(_latch));
 
        //Start service checkers using executor framework
        ExecutorService executor = Executors.newFixedThreadPool(_services.size());  //创建一个固定长度的线程池
        List<Future<Boolean>> resultList = new ArrayList&
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值