4

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();
    }
}
Anton Barinov
  • 183
  • 1
  • 3
  • 13
  • no, i've read it already. The problem that they are using the flag, and i also have readline() from server, that stops the processing and flag checking – Anton Barinov Aug 21 '18 at 15:00
  • 1
    what's serverReader? – xingbin Aug 21 '18 at 15:01
  • i updated the question with serverReader – Anton Barinov Aug 21 '18 at 15:03
  • I mean, what's it's type? Scanner? – xingbin Aug 21 '18 at 15:04
  • @Sun BufferedReader – Anton Barinov Aug 21 '18 at 15:05
  • @AntonBarinov, you need to interpret a `BufferReader#readLine` call before you close your own thread. I've added a reference to the answer about closing its socket. – Andrew Tobilko Aug 21 '18 at 15:07
  • The flag alternative will require you sending some input to the serverReader. If you don't want that, then the other alternative is to interrupt the child thread. Though it is not totally clear in the code I guess it is incomeReader. You can call interrupt on that variable, and handle the exception... – Juan Aug 21 '18 at 15:08
  • i found an answer: i have to close the socket, and it will throw an exception that stops the while block – Anton Barinov Aug 21 '18 at 15:11
  • 2
    @AndrewTobilko Is it possible that directly interrupting the thread instead of closing the socket could create leak? – Bhesh Gurung Aug 21 '18 at 15:14
  • 1
    @BheshGurung hhm, it's interesting... I don't know for sure but I think some resources may be released incorrectly/or may not be released at all, so a leak is possible. – Andrew Tobilko Aug 21 '18 at 15:37

1 Answers1

1

Swap the && order. The && operator is short-circuit, so the right side won't execute if the left side fails. The opposite is true for ||.

while (!isClosed() && readLine()) 
killjoy
  • 3,665
  • 1
  • 19
  • 16
  • as i wrote above, i tried it. it doesn't help. I think it checks the flag, it false, that it goes to the waiting for the line. It is possible not to get any other messages at all, but i still have to stop it when i want. – Anton Barinov Aug 21 '18 at 15:04
  • 2
    `isClosed()` might be turned to `true` right after the condition `!isClosed()` is validated – Andrew Tobilko Aug 21 '18 at 15:05