我创建了一个线程(服务器),该线程从输入(System.in)中读取字符串并向客户端输出,并且在我尝试关闭流时
我收到此错误:当我调用此函数closeStream()时,无法访问的代码是以下代码,请帮助:
import java.io.*;
public class ThreadWrite extends Thread{
private PrintWriter output;
private BufferedReader inputServer;
private final boolean test=true;
public ThreadWrite(PrintWriter out) {
output = out;
inputServer = new BufferedReader(new InputStreamReader(System.in));
}
public void run(){
String send;
while(test){
System.out.print("Server:");
try {
send = inputServer.readLine();
output.println(send);
} catch (IOException e) {
e.printStackTrace();
}
}
closeStream();
}
public void closeStream() throws Exception{
inputServer.close();
output.close();
}
}
需要你的帮助
最佳答案
while(test)
始终返回true,因此while循环(closeStream();
)之后的语句不可访问。