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