Is there a way to interrupt a ServerSocket while it is blocking on accept()?

Tim Rohaly

Before you invoke accept(), you should set a timeout on the ServerSocket using the setSoTimeout() method. If no connection are made within the timeout period, accept() will throw an InterruptedIOException. A thread that wishes to interrupt the accept() can set a flag which can be checked in the exception handler. If the flag isn't set simply go back to listening. Otherwise you can close the ServerSocket. The timeout value can be chosen to ensure that the interrupt gets handled in a reasonable amount of time for your application.