美文网首页
java记一次InetAddress使用卡顿

java记一次InetAddress使用卡顿

作者: 我是大导演 | 来源:发表于2019-12-16 14:37 被阅读0次

Java程序出现卡顿, 

centos7系统

定位结果出在两行代码,耗时10秒钟

InetAddress address = InetAddress.getLocalHost();

String name = address.getHostName();

代码需求是获取本机hostname


import java.net.InetAddress;

import java.net.UnknownHostException;

public class NetDemo {

    public static void main(String[] args) {

        try {

            Long start = System.currentTimeMillis();

            InetAddress address = InetAddress.getLocalHost();

            Long end = System.currentTimeMillis();

            System.out.println("ip time " + (end - start) / 1000);

            System.out.println(address.getHostAddress());

            String name = address.getHostName();

            System.out.println(name);

            end = System.currentTimeMillis();

            System.out.println("hostname time " + (end - start) / 1000);

        } catch (UnknownHostException e) {

            e.printStackTrace();

        }

    }

}


在linux 中 先获取 etc/hostname 里对应的系统名称,然后根据名称到etc/hosts里找出对应的IP

当出现hostname在hosts里找不到时, ....... 


解决方案:

将/etc/hostname里的 名称,在hosts里配置对应的IP

相关文章

网友评论

      本文标题:java记一次InetAddress使用卡顿

      本文链接:https://www.haomeiwen.com/subject/dfqdnctx.html