3
有没有简单的方法可以跳过java中的readLine()方法,如果它需要比2秒更长的时间?在java中跳过BufferedReader的readLine()方法
这里的语境中,我要问这样一个问题:
public void run()
{
boolean looping = true;
while(looping) {
for(int x = 0; x<clientList.size(); x++) {
try {
Comm s = clientList.get(x);
String str = s.recieve();
// code that does something based on the string in the line above
}
// other stuff like catch methods
}
}
}
通讯是一类我写的,和接收方法,其中包含一个名为“在”的BufferedReader,是这样的:
public String recieve()
{
try { if(active) return in.readLine(); }
catch(Exception e) { System.out.println("Comm Error 2: "+e); }
return "";
}
我注意到程序停止并且在继续之前等待输入流有东西要读。这是不好的,因为我需要程序保持循环(当它循环时,它会发送到所有其他客户端并要求输入)。如果没有东西可读,是否有办法跳过readLine()过程?
我也很确定我没有解释得很好,所以如果我感到困惑,请问我问题。
在超时的底层Reader/Stream上使用'BufferedReader'或'BufferedInputStream'也是危险的。这些缓冲类不会尝试从超时恢复,因此可能会丢失部分传输的数据包。 – 2010-05-19 00:28:08