Skip to content

Instantly share code, notes, and snippets.

@nikkatsa
Created May 13, 2016 21:59
Show Gist options
  • Save nikkatsa/6ea7baa487fd73f3e9eb4c65d4567bad to your computer and use it in GitHub Desktop.
Save nikkatsa/6ea7baa487fd73f3e9eb4c65d4567bad to your computer and use it in GitHub Desktop.
public class NamedThreadFactory implements ThreadFactory {
private final String name;
private final boolean isDaemon;
private final AtomicInteger threadCounter;
public NamedThreadFactory() {
this("Thread");
}
public NamedThreadFactory(String name) {
this(name, true);
}
public NamedThreadFactory(String name, boolean isDaemon) {
this.name = name;
this.isDaemon = isDaemon;
this.threadCounter = new AtomicInteger(1);
}
@Override
public Thread newThread(final Runnable r) {
final Thread t = new Thread();
t.setName(String.format("%s-%d", this.name, threadCounter.getAndIncrement()));
t.setDaemon(this.isDaemon);
return t;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment