Skip to content

Instantly share code, notes, and snippets.

@kjunine
Created August 9, 2012 03:48
Show Gist options
  • Select an option

  • Save kjunine/3300765 to your computer and use it in GitHub Desktop.

Select an option

Save kjunine/3300765 to your computer and use it in GitHub Desktop.
Changing thread names of Netty threads
...
ThreadRenamingRunnable
.setThreadNameDeterminer(ThreadNameDeterminer.CURRENT);
bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(new NamedThreadFactory(
"TestMessageServerBossThread")),
Executors.newCachedThreadPool(new NamedThreadFactory(
"TestMessageServerWorkerThread"))));
...
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;
}
}
@kjunine
Copy link
Author

kjunine commented Aug 9, 2012

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment