Skip to content

Instantly share code, notes, and snippets.

@hujj0615
Created December 4, 2012 10:36
Show Gist options
  • Save hujj0615/4202518 to your computer and use it in GitHub Desktop.
Save hujj0615/4202518 to your computer and use it in GitHub Desktop.
make threads have same name prefix
class TaskThreadFactory implements ThreadFactory {
final ThreadGroup group;
final AtomicInteger threadNumber = new AtomicInteger(1);
final String namePrefix;
TaskThreadFactory(String namePrefix) {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
this.namePrefix = namePrefix;
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement());
t.setDaemon(daemon);
t.setPriority(getThreadPriority());
return t;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment