Java InetAddress.getHostName()需要花费很长时间才能执行 [英] Java InetAddress.getHostName() taking a very long time to execute

查看:676
本文介绍了Java InetAddress.getHostName()需要花费很长时间才能执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下一些小代码段:

I have the following little code snippet:

        InetAddress address = InetAddress.getByName(host);
        if(address.isReachable(TIMEOUT_IN_MILLISECONDS)) {
          System.out.println(host + " is reachable.");
          String hostName = address.getHostName();
          System.out.println(hostName);
        }

如果找到了计算机,则getHostName()方法将花费相当多的时间来执行.有人可以解释为什么吗?

The getHostName() method is taking quite some time to execute if a machine has been found. Could someone please explain why?

推荐答案

来自反向主机名查找.因此,该方法调用的性能取决于JVM和目标主机的域名服务器之间的网络/技术堆栈的性能.

From the InetAddress#getHostName() javadocs, that method will perform a reverse hostname lookup. So the performance of that method call depends on the performance of the network/technology stack between the JVM and the domain name server for the target host.

简而言之,该方法将进行系统调用以执行反向查找(例如 getaddrinfo(3) ),该调用将由操作系统实施,以执行通过为您的计算机配置的名称服务器.

In brief, that method will make a system call to perform the reverse lookup (e.g. getaddrinfo(3)) and that call will be implemented by the operating system to perform the network actions required to gather the host information via the Name Server configured for your machine.

这篇关于Java InetAddress.getHostName()需要花费很长时间才能执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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