Java-连接到本地主机的速度较慢
我最近换了一台电脑,注意到有一个代码片段比平时花的时间要长得多。据我所知,代码基本上是引导Netty的(它在第三方库中)。我已经抽象出代码来演示这一点。大约需要3.6秒(对于一个线程,或者对于5个线程,大约需要15秒) 我进行了线程转储,下面是主线程的结果:Java-连接到本地主机的速度较慢,java,network-programming,netty,Java,Network Programming,Netty,我最近换了一台电脑,注意到有一个代码片段比平时花的时间要长得多。据我所知,代码基本上是引导Netty的(它在第三方库中)。我已经抽象出代码来演示这一点。大约需要3.6秒(对于一个线程,或者对于5个线程,大约需要15秒) 我进行了线程转储,下面是主线程的结果: "main" #1 prio=5 os_prio=0 tid=0x00000000024f7000 nid=0x11efc runnable [0x000000000273d000] java.lang.Thread.State: R
"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 <0x0000000717ef20d0> (a java.lang.Object)
- locked <0x0000000717ef20f0> (a java.lang.Object)
- locked <0x0000000717ef20e0> (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.<init>(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.<init>(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.<init>(NioEventLoop.java:120)
at io.netty.channel.nio.NioEventLoopGroup.newChild(NioEventLoopGroup.java:87)
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:57)
at io.netty.channel.MultithreadEventLoopGroup.<init>(MultithreadEventLoopGroup.java:49)
at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:61)
at io.netty.channel.nio.NioEventLoopGroup.<init>(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)
“main”#1优先级=5操作系统优先级=0 tid=0x00000000024f7000 nid=0x11efc可运行[0x000000000273d000]
java.lang.Thread.State:可运行
位于sun.nio.ch.Net.connect0(本机方法)
位于sun.nio.ch.Net.connect(Net.java:454)
位于sun.nio.ch.Net.connect(Net.java:446)
位于sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:648)
-锁定(一个java.lang.Object)
-锁定(一个java.lang.Object)
-锁定(一个java.lang.Object)
位于java.nio.channels.SocketChannel.open(SocketChannel.java:189)
位于sun.nio.ch.PipeImpl$Initializer$LoopbackConnector.run(PipeImpl.java:127)
位于sun.nio.ch.PipeImpl$Initializer.run(PipeImpl.java:76)
位于sun.nio.ch.PipeImpl$Initializer.run(PipeImpl.java:61)
位于java.security.AccessController.doPrivileged(本机方法)
在sun.nio.ch.PipeImpl.(PipeImpl.java:171)
在sun.nio.ch.SelectorProviderImpl.openPipe(SelectorProviderImpl.java:50)
位于java.nio.channels.Pipe.open(Pipe.java:155)
位于sun.nio.ch.WindowsSelectorImpl.(WindowsSelectorImpl.java:127)
位于sun.nio.ch.WindowsSelectorProvider.openSelector(WindowsSelectorProvider.java:44)
位于io.netty.channel.nio.NioEventLoop.openSelector(NioEventLoop.java:126)
位于io.netty.channel.nio.NioEventLoop。(NioEventLoop.java:120)
位于io.netty.channel.nio.NioEventLoopGroup.newChild(NioEventLoopGroup.java:87)
位于io.netty.util.concurrent.MultithreadEventExecutorGroup。(MultithreadEventExecutorGroup.java:57)
位于io.netty.channel.MultithreadEventLoopGroup。(MultithreadEventLoopGroup.java:49)
位于io.netty.channel.nio.NioEventLoopGroup。(NioEventLoopGroup.java:61)
位于io.netty.channel.nio.NioEventLoopGroup。(NioEventLoopGroup.java:52)
位于com.anz.axe.direct.engine.pricing.forward.Foo.main(Foo.java:12)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:498)
位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
我想知道以前是否有人遇到过这个问题。我使用的Netty版本是Netty-all-4.0.15.Final,我运行的是64位Windows 7上的Java 1.8.0_102 64位(25.102-b14混合模式)。我曾尝试禁用Windows防火墙for Java可执行文件,但没有任何区别
提前谢谢
编辑:
好啊我把范围缩小到下面的测试。在我的机器上,有时需要3秒,有时需要1毫秒。我不知道为什么。即使我指定了硬编码端口(显然随着迭代的变化),它仍然很慢
public static void main(String[] args) throws IOException {
for(int i=0; i<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);
}
}
publicstaticvoidmain(字符串[]args)引发IOException{
对于(int i=0;i
public static void main(String[] args) throws IOException {
for(int i=0; i<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);
}
}