Force thread group on threads created by Netty's DefaultThreadFactory.

As a workaround for https://github.com/netty/netty/issues/5084
This commit is contained in:
Kun Zhang 2016-04-05 15:50:14 -07:00
parent bd87b3f739
commit 6a55e2990b
1 changed files with 19 additions and 1 deletions

View File

@ -50,6 +50,7 @@ import io.netty.handler.codec.http2.Http2Headers;
import io.netty.util.AsciiString;
import io.netty.util.AttributeKey;
import io.netty.util.concurrent.DefaultThreadFactory;
import io.netty.util.concurrent.FastThreadLocalThread;
import java.io.IOException;
import java.nio.channels.ClosedChannelException;
@ -201,7 +202,8 @@ class Utils {
public EventLoopGroup create() {
// Use Netty's DefaultThreadFactory in order to get the benefit of FastThreadLocal.
boolean useDaemonThreads = true;
ThreadFactory threadFactory = new DefaultThreadFactory(name, useDaemonThreads);
ThreadFactory threadFactory = new ThreadGroupSavingDefaultThreadFactory(
name, useDaemonThreads);
int parallelism = numEventLoops == 0
? Runtime.getRuntime().availableProcessors() * 2 : numEventLoops;
return new NioEventLoopGroup(parallelism, threadFactory);
@ -218,6 +220,22 @@ class Utils {
}
}
// A workaround for https://github.com/netty/netty/issues/5084
// TODO(zhangkun83): remove it once the issue has been resolved in netty.
private static class ThreadGroupSavingDefaultThreadFactory extends DefaultThreadFactory {
final ThreadGroup threadGroup;
ThreadGroupSavingDefaultThreadFactory(String name, boolean daemon) {
super(name, daemon);
threadGroup = Thread.currentThread().getThreadGroup();
}
@Override
protected Thread newThread(Runnable r, String name) {
return new FastThreadLocalThread(threadGroup, r, name);
}
}
private Utils() {
// Prevents instantiation
}