java InetAddress.getLocalHost() 运行缓慢(30 秒以上)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33289695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
InetAddress.getLocalHost() slow to run (30+ seconds)
提问by imrichardcole
With the following code:
使用以下代码:
try {
System.out.println(new Date());
InetAddress hostName = InetAddress.getLocalHost();
System.out.println(new Date());
} catch (UnknownHostException e) {
e.printStackTrace();
}
I get this output:
我得到这个输出:
Thu Oct 22 20:58:22 BST 2015
Thu Oct 22 20:58:52 BST 2015
In other words 30 seconds to execute. Machine is 2015 Macbook Pro with Java 1.8.0_60.
换句话说,执行时间为 30 秒。机器是带有 Java 1.8.0_60 的 2015 Macbook Pro。
Why does this take so long?
为什么这需要这么长时间?
回答by imrichardcole
The issue can be solved by adding the following to /etc/hosts
(assuming output of hostname
command is my-macbook
:
该问题可以通过添加以下内容来解决/etc/hosts
(假设hostname
命令的输出是my-macbook
:
127.0.0.1 my-macbook
::1 my-macbook
This returns the time to something more suitable (< 1 second)
这会将时间返回到更合适的值(< 1 秒)
回答by Octavian R.
This problem appears on MacOS Sierra using Java8, updates equals or bigger than 60 (jdk1.8.0_60.jdk, jdk1.8.0_77.jdk, etc).
此问题出现在使用 Java8 的 MacOS Sierra 上,更新等于或大于 60(jdk1.8.0_60.jdk、jdk1.8.0_77.jdk 等)。
The solution can be found here: https://github.com/thoeni/inetTester.
可以在此处找到解决方案:https: //github.com/thoeni/inetTester。
This is the content of my /etc/hosts file:
这是我的 /etc/hosts 文件的内容:
127.0.0.1 localhost mac.local
::1 localhost mac.local
In my case, macis my computer name.
就我而言,mac是我的计算机名称。
回答by bischoje
I suspect the delay here was due to a failed attempt at DNS resolution. Perhaps your DNS servers were not configured correctly. The 30 seconds probably represents the timeout on the DNS resolution.
我怀疑这里的延迟是由于 DNS 解析尝试失败造成的。也许您的 DNS 服务器配置不正确。30 秒可能代表 DNS 解析超时。
The reason your solution improved the speed is that adding the entry to the hosts file allowed the hostname to be resolved locally and thus skip the attempt to resolve the hostname against an actual (remote) DNS server.
您的解决方案提高速度的原因是将条目添加到主机文件允许在本地解析主机名,从而跳过针对实际(远程)DNS 服务器解析主机名的尝试。
EDIT: You may wonder why this method does any host resolution at all. Apparently, it is part of an anti-spoofing mechanism built in to the Java networking library. See the accepted answer of this post for more details: InetAddress.getCanonicalHostName() returns IP instead of Hostname
编辑:您可能想知道为什么这种方法根本不进行任何主机解析。显然,它是内置于 Java 网络库的反欺骗机制的一部分。有关更多详细信息,请参阅此帖子的已接受答案:InetAddress.getCanonicalHostName() 返回 IP 而不是主机名
回答by shilaimuslm
The above answer works on my mac, you can try it like this:
上面的答案在我的mac上有效,你可以这样尝试:
step 1, download inetTester.jar from enter link description here
步骤1,从这里输入链接描述下载inetTester.jar
step 2, run it on your mac. here is the result on my mac:
第 2 步,在您的 Mac 上运行它。这是我的 mac 上的结果:
$ java -jar ./inetTester.jar
Calling the hostname resolution method...
Method called, hostname MacBook-Pro.local, elapsed time: 5009 (ms)
it takes 5s to run the test, and it shows the hostname of my mac.
运行测试需要 5 秒,它显示了我的 mac 的主机名。
step 3, modify the /etc/hosts:
第三步,修改/etc/hosts:
127.0.0.1 MacBook-Pro.local
::1 MacBook-Pro.local
the host is what shows in step 2. and after this, run the test again:
主机是第 2 步中显示的内容。在此之后,再次运行测试:
$ java -jar ./inetTester.jar
Calling the hostname resolution method...
Method called, hostname MacBook-Pro.local, elapsed time: 6 (ms)
yeah, it comes with only 6ms. 5s -> 6ms, nice.
是的,它只有 6 毫秒。5s -> 6ms,不错。
回答by Michael Remme
On a MacBook Pro with Java 1.8.0_92 and 1.80_112 this problem is still existing, the call to InetAddress.getLocalhost() needs > 5 seconds. The solution with the modified /etc/hosts does not work. Only switching back to Java 1.8.0_051 solves this problem.
在带有 Java 1.8.0_92 和 1.80_112 的 MacBook Pro 上,这个问题仍然存在,调用 InetAddress.getLocalhost() 需要 > 5 秒。修改 /etc/hosts 的解决方案不起作用。只有切换回 Java 1.8.0_051 才能解决这个问题。