Java: nice way to stop threaded TCP server? Java: nice way to stop threaded TCP server? multithreading multithreading

Java: nice way to stop threaded TCP server?


Your model works appropriately. The best way of interrupting non-interruptable constructs like IO is to close the socket. You can of course handle it before you go into a blocking state, but if the IO functions don't react to interruption you dont really have many good options


I would suggest using a boolean flag that the workers check periodically. Call the flag shouldStop and if it's set to true, the worker cleans up and then dies. Following this method would allow you to implement some clean-up code so you don't leave resources hanging, etc.


You must not simply stop the server. The shutdown process might take a while while cleanup occurs because you need to ensure consistency.

Imagine a database server, if you simply shut it down while it is carrying out transactions you may leave its data inconsistent. That's why it typically takes a while to shutdown the server.

  • You must first stop accepting newconnections in the server.
  • Then you can either wait for thecurrent worker threads to finishtheir work and then close theserver and shutdown officially.
  • Or you force the worker threads toclose their connections with the
    client (probably using some sort
    of flag as suggested). This mightimply some cleanup to leave dataconsistent, for instance reverttrasnsactions or any kind of changesyou have done in files or in memory.

Closing the connections with the clients in the server side should cause the clients to get a EOF on their sides as far as I understand.

[EDIT-1]

I have delved a bit on the issue, just because I had not used sockets in a while and because I found the question interesting. I think as it has been well pointed out by others, the only option is to close the socket, which according got Javadocs will automatically close the input and output streams/

If there are chances that the thread is not IO-blocked, but in wait state or sleeping, I think it is still recommended to issue a Thread.interrupt() for the corresponding worker thread of a given socket; because there cannot be certainty of the blocking of state of every thread.

public static class IOServerWorker implements Runnable{        private Socket socket;        public IOServerWorker(Socket socket){            this.socket = socket;        }        @Override        public void run() {            String line = null;            try{                BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));                while( (line = reader.readLine())!=null){                    System.out.println(line);                }                reader.close();            }catch(IOException e){                //TODO: do cleanup here                //TODO: log | wrap | rethrow exception            }        }    }