美文网首页
org.apache.curator.utils.Closeab

org.apache.curator.utils.Closeab

作者: Edjon | 来源:发表于2018-07-19 13:38 被阅读0次

写个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.Closeab

      本文链接:https://www.haomeiwen.com/subject/highpxtx.html