multithreading - Java: nice way to stop threaded TCP server? -
i have following structure tcp client-server communication:
- on server startup server starts acceptor thread, accepts client connections , passes serversocket it.
- when client connection arrives, acceptor thread calls accept() on serversocket , submits client processing job worker thread (by executor/thread pool) , provides client socket it.
- worker in loop reads data client socket stream, processes , sends replies.
the question how gracefully stop whole system? can stop acceptor thread closing serversocket. cause accept() blocking call throw socketexception. how stop workers? read stream , call blocking. according this streams not throw interruptedexception , worker cannot interrupt()'ed.
it looks need close worker socket thread, right? this, socket should made public field or method should provided in worker close it. nice? or may whole design flawed?
your model works appropriately. best way of interrupting non-interruptable constructs io close socket. can of course handle before go blocking state, if io functions don't react interruption dont have many options
Comments
Post a Comment