title: Java - slow to connect to localhost
body: <p>I have recently changed PC, and noticed that there is a code snippet that is taking a lot longer than usual. As far as I can tell, the code is basically bootstrapping Netty (it is within thirdparty library). I've abstracted out the code to demonstrate this. It takes about 3.6 seconds  (for one thread, or about 15 seconds for 5 threads)</p>
  
  <pre><code>import io.netty.channel.nio.NioEventLoopGroup;
  
  import java.util.concurrent.ThreadFactory;
  import java.util.concurrent.atomic.AtomicInteger;
  
  public class Foo {
  
      public static void main(String[] args) {
          final long stsart = System.currentTimeMillis();
          NioEventLoopGroup workers = new NioEventLoopGroup(1, new NamedThreadFactory("Worker-", true));
          final long finish = System.currentTimeMillis();
          System.out.println(finish - stsart);
      }
  
  
      public static class NamedThreadFactory implements ThreadFactory {
          private final String prefix;
          private final boolean daemon;
          private final AtomicInteger threadNumber = new AtomicInteger(0);
  
          public NamedThreadFactory(String prefix, boolean daemon) {
              this.prefix = prefix;
              this.daemon = daemon;
          }
  
          public Thread newThread(Runnable r) {
              Thread t = new Thread(r, this.prefix + this.threadNumber.incrementAndGet());
              t.setDaemon(daemon);
              return t;
          }
      }
  }
  </code></pre>
  
  <p>I did a thread dump, and here is the outcome  for the main thread:</p>
  
  <pre><code>"main" #1 prio=5 os_prio=0 tid=0x00000000024f7000 nid=0x11efc runnable [0x000000000273d000]
     java.lang.Thread.State: RUNNABLE
      at sun.nio.ch.Net.connect0(Native Method)
      at sun.nio.ch.Net.connect(Net.java:454)
      at sun.nio.ch.Net.connect(Net.java:446)
      at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:648)
      - locked &lt;0x0000000717ef20d0&gt; (a java.lang.Object)
      - locked &lt;0x0000000717ef20f0&gt; (a java.lang.Object)
      - locked &lt;0x0000000717ef20e0&gt; (a java.lang.Object)
      at java.nio.channels.SocketChannel.open(SocketChannel.java:189)
      at sun.nio.ch.PipeImpl$Initializer$LoopbackConnector.run(PipeImpl.java:127)
      at sun.nio.ch.PipeImpl$Initializer.run(PipeImpl.java:76)
      at sun.nio.ch.PipeImpl$Initializer.run(PipeImpl.java:61)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.nio.ch.PipeImpl.&lt;init&gt;(PipeImpl.java:171)
      at sun.nio.ch.SelectorProviderImpl.openPipe(SelectorProviderImpl.java:50)
      at java.nio.channels.Pipe.open(Pipe.java:155)
      at sun.nio.ch.WindowsSelectorImpl.&lt;init&gt;(WindowsSelectorImpl.java:127)
      at sun.nio.ch.WindowsSelectorProvider.openSelector(WindowsSelectorProvider.java:44)
      at io.netty.channel.nio.NioEventLoop.openSelector(NioEventLoop.java:126)
      at io.netty.channel.nio.NioEventLoop.&lt;init&gt;(NioEventLoop.java:120)
      at io.netty.channel.nio.NioEventLoopGroup.newChild(NioEventLoopGroup.java:87)
      at io.netty.util.concurrent.MultithreadEventExecutorGroup.&lt;init&gt;(MultithreadEventExecutorGroup.java:57)
      at io.netty.channel.MultithreadEventLoopGroup.&lt;init&gt;(MultithreadEventLoopGroup.java:49)
      at io.netty.channel.nio.NioEventLoopGroup.&lt;init&gt;(NioEventLoopGroup.java:61)
      at io.netty.channel.nio.NioEventLoopGroup.&lt;init&gt;(NioEventLoopGroup.java:52)
      at com.anz.axle.direct.engine.pricing.forward.Foo.main(Foo.java:12)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
  </code></pre>
  
  <p>I'm wondering if anyone has ever run into this problem before. The version of Netty I'm using is netty-all-4.0.15.Final, and I'm running on Java 1.8.0_102 64-Bit (25.102-b14 mixed mode) on Windows 7 64 bit. I've tried disabling Windows Firewall for Java executable, and that didnt make any difference..</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>EDIT:</strong>
  Ok.. I narrow it down to the following test. On my machine, sometimes it can take 3 seconds, sometime it takes 1 millisecond. I'm not sure why. Even if I specified hardcoded port (changing with the iteration, obviously), it is still slow..</p>
  
  <pre><code>  public static void main(String[] args) throws IOException {
          for(int i=0; i&lt;20;i++) {
              final InetAddress address = InetAddress.getByName("127.0.0.1");
  
              final ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
              serverSocketChannel.socket().bind(new InetSocketAddress(address, 0));
  
              final long start = System.currentTimeMillis();
              final InetSocketAddress inetSocketAddress = new InetSocketAddress(address, serverSocketChannel.socket().getLocalPort());
              SocketChannel.open(inetSocketAddress);
              final long finish = System.currentTimeMillis();
              System.out.println(finish - start);
          }
      }
  </code></pre>