[英]Stop Thread that is blocked reading from a readLine() method
while (active) {
String message = reader.readLine();
}
public void stop(){
active = false;
}
How do i stop this Thread
before readLine()
return a String
?如何在
readLine()
返回String
之前停止此Thread
?
In this case, your BufferedReader has to be closed to unblock the reading thread.在这种情况下,必须关闭 BufferedReader 以解除对读取线程的阻塞。
public void stop() {
closeQuietly(reader); // unblock
active = false;
}
public static void closeQuietly(Closable c) {
if(c != null)
try {
c.close();
} catch(IOException ignored) { }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.