strolch/utils/src/main/java/li/strolch/utils/NamedThreadPoolFactory.java

28 lines
848 B
Java

package li.strolch.utils;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Simple {@link ThreadFactory} which allocates as a pool and has a name for each pool
*/
public class NamedThreadPoolFactory implements ThreadFactory {
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String poolName;
public NamedThreadPoolFactory(String poolName) {
this.group = Thread.currentThread().getThreadGroup();
this.poolName = poolName + "-";
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(this.group, r, this.poolName + this.threadNumber.getAndIncrement(), 0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}