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&