InetAddress.getLocalHost()运行缓慢(30+秒) [英] InetAddress.getLocalHost() slow to run (30+ seconds)

查看:668
本文介绍了InetAddress.getLocalHost()运行缓慢(30+秒)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码:

try {
  System.out.println(new Date());
  InetAddress hostName = InetAddress.getLocalHost();
  System.out.println(new Date());
} catch (UnknownHostException e) {
  e.printStackTrace();
}

我得到这个输出:

Thu Oct 22 20:58:22 BST 2015
Thu Oct 22 20:58:52 BST 2015

换句话说,执行30秒。 Machine是2015 Macbook Pro with Java 1.8.0_60。

In other words 30 seconds to execute. Machine is 2015 Macbook Pro with Java 1.8.0_60.

为什么这需要这么长时间?

Why does this take so long?

推荐答案

可以通过在/ etc / hosts中添加以下内容来解决此问题(假设主机名为 macbook

The issue can be solved by adding the following to /etc/hosts (assuming hostname is macbook:

127.0.0.1   macbook
::1         macbook

这会将时间返回到更合适的时间(< 1秒)

This returns the time to something more suitable (< 1 second)

这篇关于InetAddress.getLocalHost()运行缓慢(30+秒)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆