public static int[] findLocalPorts(int portNum) {
        int[] ports = new int[portNum];
        List<ServerSocket> sockets = new ArrayList<>();
        for (int i = 0; i < portNum; i++) {
            int port = -1;
            while (port < 0) {
                try {
                    ServerSocket socket = new ServerSocket(0);
                    socket.setReuseAddress(true);
                    port = socket.getLocalPort();
                    ports[i] = port;
                    sockets.add(socket);
                } catch (IOException ie) {
                    // let it go.
                }
            }
        }
        for (ServerSocket socket : sockets) {
            try {
                socket.close();
            } catch (IOException e) {
                // Ignore IOException on close()
            }
        }
        return ports;
    }
Find a local port.

@return A local port to use.
core/src/test/java/kafka/autobalancer/utils/AutoBalancerTestUtils.java