Java程序出現(xiàn)卡頓,?
centos7系統(tǒng)
定位結(jié)果出在兩行代碼,耗時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?里對應的系統(tǒng)名稱,然后根據(jù)名稱到etc/hosts里找出對應的IP
當出現(xiàn)hostname在hosts里找不到時, .......?
解決方案:
將/etc/hostname里的?名稱,在hosts里配置對應的IP