I am trying to terminate the child thread, that's waiting for the input from the server. I've tried to do it with the flag, but it stops on the
message = serverReader.readLine() && !isClosed()
,
because it waits for the input from the server and only after this input it checks the flag and interrupts. But it's not a good solution, cause i couldn't stop it without server message.
I also tried to check firstly the flag and then readline(), but it doesn't work too.
Is it any good solutions for that?
@Override
public void run() {
String message;
try {
while((message = serverReader.readLine()) != null && !isClosed()) {
consoleWriter.println("Other user: " + message);
consoleWriter.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void setUpNetworking() {
try {
Socket socket = new Socket("127.0.0.1", 5000);
InputStreamReader streamReader = new InputStreamReader(socket.getInputStream());
serverWriter = new PrintWriter(socket.getOutputStream());
serverReader = new BufferedReader(streamReader);
//Starting listening messages from server
incomeReader = new Thread(new IncomeMessagesReader(serverReader, consoleWriter, this));
incomeReader.start();
System.out.println("Networking established");
} catch (IOException e) {
e.printStackTrace();
}
}