org.apache.curator.utils.CloseableExecutorService 默认线程

写个main方法测试Curator LeaderSelector的时候发现个奇怪的事情:

如果主线程很快结束,而没有sleep或者死循环,LeaderSelector 的takeLeadership 线程也会很快退出来。

找了很久才发现它的线程池新开线程设置成了demon。

public LeaderSelector(CuratorFramework client, String leaderPath, LeaderSelectorListener listener){

this(client, leaderPath,new CloseableExecutorService(Executors.newSingleThreadExecutor(defaultThreadFactory),true), listener);

}

private static final ThreadFactorydefaultThreadFactory = ThreadUtils.newThreadFactory("LeaderSelector");

public static ThreadFactory newThreadFactory(String processName)

{

return newGenericThreadFactory("Curator-" + processName);

}

public static ThreadFactory newGenericThreadFactory(String processName)

{

Thread.UncaughtExceptionHandler uncaughtExceptionHandler =new Thread.UncaughtExceptionHandler()

{

@Override

        public void uncaughtException(Thread t, Throwable e)

{

log.error("Unexpected exception in thread: " + t, e);

Throwables.propagate(e);

}

};

return new ThreadFactoryBuilder()

.setNameFormat(processName +"-%d")

.setDaemon(true)

.setUncaughtExceptionHandler(uncaughtExceptionHandler)

.build();

}

你可能感兴趣的:(org.apache.curator.utils.CloseableExecutorService 默认线程)