//使用静态内部类初始化线程池privatestaticclassLazyHolder{privatefinalstaticStringCORE_THREAD="DynamicServerListLoadBalancer.ThreadPoolSize";privatefinalstaticDynamicIntProperty poolSizeProp =newDynamicIntProperty(CORE_THREAD,2);privatestaticThread _shutdownThread;staticScheduledThreadPoolExecutor _serverListRefreshExecutor =null;static{int coreSize = poolSizeProp.get();ThreadFactory factory =(newThreadFactoryBuilder()).setNameFormat("PollingServerListUpdater-%d").setDaemon(true).build();//创建线程池
_serverListRefreshExecutor =newScheduledThreadPoolExecutor(coreSize, factory);
poolSizeProp.addCallback(newRunnable(){@Overridepublicvoidrun(){
_serverListRefreshExecutor.setCorePoolSize(poolSizeProp.get());}});
_shutdownThread =newThread(newRunnable(){publicvoidrun(){
logger.info("Shutting down the Executor Pool for PollingServerListUpdater");//关闭线程池shutdownExecutorPool();}});//虚拟机关闭钩子方法Runtime.getRuntime().addShutdownHook(_shutdownThread);}privatestaticvoidshutdownExecutorPool(){if(_serverListRefreshExecutor !=null){//关闭线程池
_serverListRefreshExecutor.shutdown();if(_shutdownThread !=null){try{//关闭线程Runtime.getRuntime().removeShutdownHook(_shutdownThread);}catch(IllegalStateException ise){// NOPMD// this can happen if we're in the middle of a real// shutdown,// and that's 'ok'}}}}}//获取线程池实例privatestaticScheduledThreadPoolExecutorgetRefreshExecutor(){returnLazyHolder._serverListRefreshExecutor;}