Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
private AtomicInteger poolIdProvider = new AtomicInteger(0);
public ThreadFactory getThreadFactory(String name) {
int poolId = poolIdProvider.getAndIncrement();
//grab security group for use in all threads from this factory
SecurityManager s = System.getSecurityManager();
ThreadGroup group = s != null ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
return new ThreadFactory() {
private AtomicInteger threadIdProvider = new AtomicInteger(0);
public Thread newThread(@NotNull Runnable r) {
Thread thread = new Thread(group, r, threadName(name, poolId, threadIdProvider.getAndIncrement()));
return thread;
private String threadName(String name, Integer poolId, Integer threadId) {
return String.join("-",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.