Java InetAddress.getHostName() taking a very long time to execute

后端 未结 3 559
暗喜
暗喜 2021-01-13 01:28

I have the following little code snippet:

        InetAddress address = InetAddress.getByName(host);
        if(address.isReachable(TIMEOUT_IN_MILLISECONDS))         


        
相关标签:
3条回答
  • 2021-01-13 01:39

    toString() seems to be faster:

    given an InetAddress ia or InterfaceAddress ia,

    System.out.println ( ia.toString() ) will show a string containing your ipAddress faster than ia.getHostName()

    You can then use ia.toString().substring to extract it.

    I don't know why.

    0 讨论(0)
  • 2021-01-13 01:43

    Some of the addresses need longer time to be resolved. InetAddress has a cache to store successful and unsuccessful resolutions. Also, make a threadpool. You can improve the performance

    0 讨论(0)
  • 2021-01-13 01:59

    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.

    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.

    0 讨论(0)
提交回复
热议问题