为什么此循环第一次执行,然后等到if子句中的条件为true?

Random rand = new Random();
int i = 0;
while (true) {
    currencyList.put(Currency.EUR, BigDecimal.valueOf(7*rand.nextDouble()+1.2));
    currencyList.put(Currency.HRK, BigDecimal.valueOf(7*rand.nextDouble()+1.2));
    currencyList.put(Currency.NOK, BigDecimal.valueOf(7*rand.nextDouble()+1.2));
    currencyList.put(Currency.USD, BigDecimal.valueOf(7*rand.nextDouble()+1.2));
    System.out.println(currencyList);
    Socket sock = null;
    if ( (sock = serversocket.accept()) != null) {
        System.out.println("Client " + (++i) + "connected...");
        new ServerThreadB(sock,username).start();
    }
}

最佳答案

阅读文档,上面写着“方法将阻塞,直到建立连接为止。”:https://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html#accept()

10-02 02:10