readable thread names
authorAlexey Kudravtsev <cdr@intellij.com>
Thu, 29 Oct 2015 12:09:20 +0000 (15:09 +0300)
committerAlexey Kudravtsev <cdr@intellij.com>
Thu, 29 Oct 2015 12:16:01 +0000 (15:16 +0300)
platform/platform-impl/src/org/jetbrains/io/NettyUtil.java

index a1a252e10a5945ecbc0ad9d6b541cd77f5d27a4f..8ecf0a43bbe974ff4fe8c9dc49e159c3ba7b8941 100644 (file)
@@ -34,6 +34,7 @@ import io.netty.handler.codec.http.HttpResponseEncoder;
 import io.netty.handler.codec.http.cors.CorsConfig;
 import io.netty.handler.codec.http.cors.CorsHandler;
 import io.netty.handler.stream.ChunkedWriteHandler;
+import io.netty.util.ThreadDeathWatcher;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.jetbrains.concurrency.AsyncPromise;
@@ -46,6 +47,7 @@ import java.net.ConnectException;
 import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.util.Random;
+import java.util.concurrent.CountDownLatch;
 
 public final class NettyUtil {
   public static final int MAX_CONTENT_LENGTH = 100 * 1024 * 1024;
@@ -178,9 +180,39 @@ public final class NettyUtil {
   public static ServerBootstrap nioServerBootstrap(@NotNull EventLoopGroup eventLoopGroup) {
     ServerBootstrap bootstrap = new ServerBootstrap().group(eventLoopGroup).channel(eventLoopGroup instanceof NioEventLoopGroup ? NioServerSocketChannel.class : EpollServerSocketChannel.class);
     bootstrap.childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true);
+    changeThreadDeathWatcherNameTo("Netty thread death watcher"); // "threadDeathWatcher-2-1"? Is this really all that you got, Netty?
     return bootstrap;
   }
 
+  private static void changeThreadDeathWatcherNameTo(@NotNull final String name) {
+    final CountDownLatch latch = new CountDownLatch(1);
+    Thread thread = new Thread("temp") {
+      @Override
+      public void run() {
+        try {
+          latch.await();
+        }
+        catch (InterruptedException e) {
+          throw new RuntimeException(e);
+        }
+      }
+    };
+    thread.start();
+    ThreadDeathWatcher.watch(thread, new Runnable() {
+      @Override
+      public void run() {
+        Thread.currentThread().setName(name);
+      }
+    });
+    latch.countDown();
+    try {
+      thread.join();
+    }
+    catch (InterruptedException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
   @SuppressWarnings("unused")
   @Deprecated
   @NotNull