如果需要的时间超过 2 秒,是否有一种简单的方法可以跳过 java 中的 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
}
}
}
Comm是我写的一个类,receive方法,里面包含一个叫“in”的BufferedReader,是这样的:
public String recieve()
{
try { if(active) return in.readLine(); }
catch(Exception e) { System.out.println("Comm Error 2: "+e); }
return "";
}
我注意到程序停止并等待输入流在继续之前有要读取的内容。这很糟糕,因为我需要程序继续循环(当它循环时,它会转到所有其他客户端并要求输入)。如果没有什么可读的,有没有办法跳过 readLine() 过程?
我也很确定我没有很好地解释这一点,所以如果我感到困惑,请向我提问。