在java的并发包中java.util.concurrent
已经有很多线程池的实现,所有线程池的实现都实现于java.util.concurrent
接口:
在fescar中直接并最常使用到的线程池有:
ThreadPoolExecutor
:最核心的线程池实现。
ScheduledThreadPoolExecutor
:继承于ThreadPoolExecutor
,主要用来在给定的延迟之后运行任务或者定期执行任务。
所有线程池都可以传入一个java.util.concurrent.ThreadFactory
接口的线程工厂实现类。如果不传会使用一个默认工厂类,如:
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
所有的线程工厂类需要实现java.util.concurrent.ThreadFactory
接口,默认的线程工厂实现类的线程名称是以"pool-"为前缀,中间一个序列码,"-thread-"为后缀。
static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
在fescar中使用了很多的线程池,如果所有线程名称都是这种名称,比较难于调试与监控。所以需要自定义线程工厂类,可以定义线程名称。所以就实现了一个NamedThreadFactory
:
public class NamedThreadFactory implements ThreadFactory
{
private static final AtomicInteger POOL_SEQ = new AtomicInteger(1);
private final AtomicInteger mThreadNum = new AtomicInteger(1);
private final String mPrefix;
private final boolean mDaemo;
private final ThreadGroup mGroup;
public NamedThreadFactory()
{
this("pool-" + POOL_SEQ.getAndIncrement(),false);
}
public NamedThreadFactory(String prefix)
{
this(prefix,false);
}
public NamedThreadFactory(String prefix,boolean daemo)
{
mPrefix = prefix + "-thread-";
mDaemo = daemo;
SecurityManager s = System.getSecurityManager();
mGroup = ( s == null ) ? Thread.currentThread().getThreadGroup() : s.getThreadGroup();
}
public Thread newThread(Runnable runnable)
{
String name = mPrefix + mThreadNum.getAndIncrement();
Thread ret = new Thread(mGroup,runnable,name,0);
ret.setDaemon(mDaemo);
return ret;
}
public ThreadGroup getThreadGroup()
{
return mGroup;
}
}
从代码中可以看到,如果创建NamedThreadFactory
实例时,如果传入线程名称前缀,如:
new NamedThreadFactory("RetryRollbacking", 1)
名称将为:
RetryRollbacking-thread-${NUMBER}
如果不传入线程名称,则默认为:
pool-${NUMBER}-thread