繁体   English   中英

虽然真实循环有时会中断

[英]While-true loop lopping sometimes

为什么这个循环执行的第一次,然后等待 ,直到条件if条款是真的吗?

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()

ServerSocket会阻止程序继续运行,直到建立连接为止。

要限制它停止程序的时间,可以使用以毫秒为单位的setSoTimeout(long milli)方法来确定直到套接字放弃为止所花费的时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM