功能
按照“PoolName-thread-n”的格式命名线程池中的线程;
示例代码
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 类说明:线程池命名工具类
*
* @author ruipeng.lrp
* @since 2017/11/23
*
**/
public class NamedThreadFactory implements ThreadFactory {
private final AtomicInteger threadNumber = new AtomicInteger(1);
private String poolName;
private boolean isDaemon;
public NamedThreadFactory() {
this("Pool");
}
public NamedThreadFactory(String poolName) {
this(poolName, true);
}
public NamedThreadFactory(String poolName, boolean isDaemon) {
this.poolName = poolName;
this.isDaemon = isDaemon;
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread();
//线程名称:PoolName-thread-n
t.setName(this.poolName + "-thread-" + threadNumber.getAndIncrement());
t.setDaemon(isDaemon);
return t;
}
}