Reputation: 105
i create a thread (server) who read string from the input (System.in) and make an output to the client and when i try to close streams i get this error : unreachable code when i call this function closeStream() here is the code please help:
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();
}
}
need your help
Upvotes: 1
Views: 411
Reputation: 387
You have mentioned closeStream()
outside the while loop
and condition of is always true and there is no statement in your while loop that breaks it and reaches closeStream()
method.
Upvotes: 0
Reputation: 12009
Test is a final variable which is true. You never leave the
while(test)
loop.
Upvotes: 0
Reputation: 74046
You declared test
as final
, so its value can not be changed and will remain true
.
So inside the while
condition just the true
remains and there is no way, that while can ever end (other than Exceptions etc.). Thus the line after the while
can not be reached.
Upvotes: 1
Reputation: 77904
You never change boolean test
to false
and you can't (since its final) therefore you never move out from while
loop.
Java
Upvotes: 1
Reputation: 393811
while(test)
always returns true, so the statement following the while loop (closeStream();
) is not reachable.
Upvotes: 2