Created
May 8, 2013 06:34
-
-
Save wsky/5538641 to your computer and use it in GitHub Desktop.
NamedThreadFactory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NamedThreadFactory implements ThreadFactory { | |
static final AtomicInteger poolNumber = new AtomicInteger(1); | |
final AtomicInteger threadNumber = new AtomicInteger(1); | |
final ThreadGroup group; | |
final String prefix; | |
final boolean isDaemon; | |
final int priority; | |
public NamedThreadFactory() { | |
this("pool"); | |
} | |
public NamedThreadFactory(String name) { | |
this(name, false, Thread.NORM_PRIORITY); | |
} | |
public NamedThreadFactory(String prefix, boolean isDaemon, int priority) { | |
SecurityManager s = System.getSecurityManager(); | |
this.group = (s != null) ? | |
s.getThreadGroup() : | |
Thread.currentThread().getThreadGroup(); | |
this.prefix = prefix + "-" + poolNumber.getAndIncrement() + "-thread-"; | |
this.isDaemon = isDaemon; | |
this.priority = priority; | |
} | |
public Thread newThread(Runnable r) { | |
Thread t = new Thread(group, r, prefix + threadNumber.getAndIncrement(), 0); | |
t.setDaemon(isDaemon); | |
t.setPriority(priority); | |
return t; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment