Skip to content

Instantly share code, notes, and snippets.

Embed
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);
@Override
public Thread newThread(@NotNull Runnable r) {
Thread thread = new Thread(group, r, threadName(name, poolId, threadIdProvider.getAndIncrement()));
thread.setDaemon(true);
return thread;
}
};
}
private String threadName(String name, Integer poolId, Integer threadId) {
return String.join("-",
name,
"pool",
String.valueOf(poolId),
"thread",
String.valueOf(threadId));
}
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.