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.