正确使用HostAddress

    String[] strs = "10.232.36.81,10.232.36.82,10.232.36.83".split(",");

        //下面这个很快
        long t1 = System.nanoTime();
        for (String str : strs) {
            InetSocketAddress isa = new InetSocketAddress(str, 2181);
            System.out.println(isa.getAddress().getHostAddress());
        }

        long t2 = System.nanoTime();
        System.out.println(t2 - t1);

        System.out.println();

        //下面这个很慢
        t1 = System.nanoTime();
        for (String str : strs) {
            InetSocketAddress isa = new InetSocketAddress(str, 2181);
            System.out.println(isa.getHostName());
        }

        t2 = System.nanoTime();
        System.out.println(t2 - t1);

 输出:

写道
10.232.36.81
10.232.36.82
10.232.36.83
2710422

10.232.36.81
10.232.36.82
10.232.36.83
15013571029

 

你可能感兴趣的:(host)