Created
March 14, 2018 16:41
-
-
Save troyunverdruss/e1e5a9bd35bffe533dae249ead294558 to your computer and use it in GitHub Desktop.
This file contains 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
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