mirror of https://github.com/grpc/grpc-java.git
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:
parent
bd87b3f739
commit
6a55e2990b
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue