1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-08-13 17:03:45 -04:00
filebot/source/net/filebot/util/DefaultThreadFactory.java
Reinhard Pointner 8299e849aa * Format Source
2015-07-25 22:47:19 +00:00

58 lines
1.2 KiB
Java

package net.filebot.util;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
public class DefaultThreadFactory implements ThreadFactory {
private final AtomicInteger threadNumber = new AtomicInteger(0);
private final ThreadGroup group;
private final int priority;
private final boolean daemon;
public DefaultThreadFactory(String name) {
this(name, Thread.NORM_PRIORITY);
}
public DefaultThreadFactory(String name, int priority) {
this(name, priority, false);
}
public DefaultThreadFactory(String groupName, int priority, boolean daemon) {
SecurityManager sm = System.getSecurityManager();
ThreadGroup parentGroup = (sm != null) ? sm.getThreadGroup() : Thread.currentThread().getThreadGroup();
this.group = new ThreadGroup(parentGroup, groupName);
this.daemon = daemon;
this.priority = priority;
}
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(group, r, String.format("%s-thread-%d", group.getName(), threadNumber.incrementAndGet()));
if (daemon != thread.isDaemon())
thread.setDaemon(daemon);
if (priority != thread.getPriority())
thread.setPriority(priority);
return thread;
}
public ThreadGroup getThreadGroup() {
return group;
}
}