柚子快報(bào)邀請(qǐng)碼778899分享:ribbon源碼中線程是的使用
柚子快報(bào)邀請(qǐng)碼778899分享:ribbon源碼中線程是的使用
源碼類(lèi):PollingServerListUpdater
使用單例模式(靜態(tài)內(nèi)部類(lèi)),初始化線程池
//使用靜態(tài)內(nèi)部類(lèi)初始化線程池
private static class LazyHolder {
private final static String CORE_THREAD = "DynamicServerListLoadBalancer.ThreadPoolSize";
private final static DynamicIntProperty poolSizeProp = new DynamicIntProperty(CORE_THREAD, 2);
private static Thread _shutdownThread;
static ScheduledThreadPoolExecutor _serverListRefreshExecutor = null;
static {
int coreSize = poolSizeProp.get();
ThreadFactory factory = (new ThreadFactoryBuilder())
.setNameFormat("PollingServerListUpdater-%d")
.setDaemon(true)
.build();
//創(chuàng)建線程池
_serverListRefreshExecutor = new ScheduledThreadPoolExecutor(coreSize, factory);
poolSizeProp.addCallback(new Runnable() {
@Override
public void run() {
_serverListRefreshExecutor.setCorePoolSize(poolSizeProp.get());
}
});
_shutdownThread = new Thread(new Runnable() {
public void run() {
logger.info("Shutting down the Executor Pool for PollingServerListUpdater");
//關(guān)閉線程池
shutdownExecutorPool();
}
});
//虛擬機(jī)關(guān)閉鉤子方法
Runtime.getRuntime().addShutdownHook(_shutdownThread);
}
private static void shutdownExecutorPool() {
if (_serverListRefreshExecutor != null) {
//關(guān)閉線程池
_serverListRefreshExecutor.shutdown();
if (_shutdownThread != null) {
try {
//關(guān)閉線程
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'
}
}
}
}
}
//獲取線程池實(shí)例
private static ScheduledThreadPoolExecutor getRefreshExecutor() {
return LazyHolder._serverListRefreshExecutor;
}
柚子快報(bào)邀請(qǐng)碼778899分享:ribbon源碼中線程是的使用
相關(guān)閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。