Fixed NETTY-336 Fine-grained control over thread renaming

NamedThreadFactory now can set the daemon flag and the thread priority
This commit is contained in:
Trustin Lee 2011-03-29 16:12:55 +09:00
parent 83bc3e794a
commit 89de2e4b95

View File

@ -39,15 +39,38 @@ public class NamedThreadFactory implements ThreadFactory {
private final ThreadGroup group; private final ThreadGroup group;
private final AtomicInteger threadId = new AtomicInteger(1); private final AtomicInteger threadId = new AtomicInteger(1);
private final String prefix; private final String prefix;
private final boolean daemon;
private final int priority;
/** /**
* Creates a new factory that creates a {@link Thread} with the specified name prefix. * Creates a new factory that creates a {@link Thread} with the specified name prefix.
*
* @param prefix the prefix of the new thread's name
*/ */
public NamedThreadFactory(String prefix) { public NamedThreadFactory(String prefix) {
this(prefix, false, Thread.NORM_PRIORITY);
}
/**
* Creates a new factory that creates a {@link Thread} with the specified name prefix.
*
* @param prefix the prefix of the new thread's name
* @param daemon {@code true} if the new thread is a daemon thread
* @param priority the priority of the new thread
*/
public NamedThreadFactory(String prefix, boolean daemon, int priority) {
if (prefix == null) { if (prefix == null) {
throw new NullPointerException("prefix"); throw new NullPointerException("prefix");
} }
if (priority < Thread.MIN_PRIORITY || priority > Thread.MAX_PRIORITY) {
throw new IllegalArgumentException(
"priority: " + priority +
" (expected: >= " + Thread.MIN_PRIORITY + " && <= " + Thread.MAX_PRIORITY);
}
this.prefix = prefix; this.prefix = prefix;
this.daemon = daemon;
this.priority = priority;
SecurityManager s = System.getSecurityManager(); SecurityManager s = System.getSecurityManager();
if (s != null) { if (s != null) {
@ -63,13 +86,8 @@ public class NamedThreadFactory implements ThreadFactory {
*/ */
public Thread newThread(Runnable r) { public Thread newThread(Runnable r) {
Thread t = new Thread(group, r, prefix + threadId.getAndIncrement()); Thread t = new Thread(group, r, prefix + threadId.getAndIncrement());
if (t.isDaemon()) { t.setDaemon(daemon);
t.setDaemon(false); t.setPriority(priority);
}
if (t.getPriority() != Thread.NORM_PRIORITY) {
t.setPriority(Thread.NORM_PRIORITY);
}
return t; return t;
} }
} }