Created
August 9, 2012 03:48
-
-
Save kjunine/3300765 to your computer and use it in GitHub Desktop.
Changing thread names of Netty threads
This file contains hidden or 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
| ... | |
| ThreadRenamingRunnable | |
| .setThreadNameDeterminer(ThreadNameDeterminer.CURRENT); | |
| bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory( | |
| Executors.newCachedThreadPool(new NamedThreadFactory( | |
| "TestMessageServerBossThread")), | |
| Executors.newCachedThreadPool(new NamedThreadFactory( | |
| "TestMessageServerWorkerThread")))); | |
| ... |
This file contains hidden or 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
| package swiftify.channel.util.execute; | |
| import java.util.concurrent.ThreadFactory; | |
| import java.util.concurrent.atomic.AtomicInteger; | |
| public class NamedThreadFactory implements ThreadFactory { | |
| private final String name; | |
| private final AtomicInteger index = new AtomicInteger(1); | |
| public NamedThreadFactory(String name) { | |
| this.name = name + "-"; | |
| } | |
| @Override | |
| public Thread newThread(Runnable r) { | |
| Thread thread = new Thread(r, name + index.getAndIncrement()); | |
| if (thread.isDaemon()) | |
| thread.setDaemon(false); | |
| if (thread.getPriority() != Thread.NORM_PRIORITY) | |
| thread.setPriority(Thread.NORM_PRIORITY); | |
| return thread; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The point of this is to use a custom thread factory that generates a thread with custom name and to call ThreadRenamingRunnable.setThreadNameDererminer(ThreadName.Determiner.CURRENT);