在之前的测试中我们使用到了一个自定义的线程工厂类来为线程命名:
public class ThreadFactoryImpl implements ThreadFactory { private final AtomicLong threadIndex = new AtomicLong(0); private final String threadNamePrefix; private final boolean daemon; public ThreadFactoryImpl(final String threadNamePrefix) { this(threadNamePrefix, false); } public ThreadFactoryImpl(final String threadNamePrefix, boolean daemon) { this.threadNamePrefix = threadNamePrefix; this.daemon = daemon; } @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r, threadNamePrefix + this.threadIndex.incrementAndGet()); thread.setDaemon(daemon); return thread; } }这是一个非常好的实践,对于复杂的项目一定有N多个线程池,有了明确的容易识别的命名,更容易在出现问题的时候进行排查。
Tomcat线程池Tomcat线程池基于原生线程池做了一些改进,主要在三方面:
一开始就初始化核心线程,之后这些线程肯定会用满,所以何不一开始就初始化呢