Java NetworkInterface.getByInetAddress()需要太长

时间:2016-02-21 20:51:27

标签: java jvm

我有一个非常奇怪的情况......我正在运行这样的java:

java -Dfile.encoding=UTF-8 -server -classpath "...jar" -verbose com.class args

我可以看到它减速20秒后:

[Loaded java.net.DefaultInterface from jdk\jre\lib\rt.jar]

之前:[Loaded java.net.NetworkInterface$1 from jdk\jre\lib\rt.jar]

可能是什么问题?提前感谢您的帮助

编辑:

on verbose:jni我可以看到它停止:

[Dynamic-linking native method java.net.NetworkInterface.getByInetAddress0 ... JNI]

所以我执行了以下程序:

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

class HelloWorldApp {
    public static void main(String[] args) {
    InetAddress ip;
    try {
        ip = InetAddress.getLocalHost();
        System.out.println("Current IP address : " + ip.getHostAddress());
        long startTime = System.nanoTime();
        NetworkInterface network = NetworkInterface.getByInetAddress(ip);
        byte[] mac = network.getHardwareAddress();
        long endTime = System.nanoTime();
        long duration = (endTime - startTime);
        double seconds = (double)duration / 1000000000.0;
        System.out.println("Duration: " +String.valueOf(seconds) + " seconds");
        System.out.print("Current MAC address : ");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));        
        }
        System.out.println(sb.toString());
        } catch (UnknownHostException e) {          
            e.printStackTrace();
        } catch (SocketException e){
            e.printStackTrace();        
        }
    }
}

输出:

Current IP address : 192.168.148.38
Duration: 20.188660558 seconds
Current MAC address : 28-B2-BD-46-20-67

NetworkInterface.getByInetAddress(ip)大约需要20秒。有什么想法吗?

java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

javac -version
javac 1.7.0_79

0 个答案:

没有答案