When a Thread is interrupted while it’s also blocking on input from a Socket’s InputStream, do the resources of that Socket (e.g. the local port) get released?
I know the Socket has an option called SoTimeout. If I set the SoTimeout option, does the port that the Socket is bound to get released when the timeout expires?
There is nothing that says that
Thread.interrupt()will interrupt the read at all, let alone close the socket, unless it is anInterruptibleChannel, in which case the semantics are clearly documented that aClosedByInterruptExceptionis thrown and the channel is closed.If you set a socket timeout and get a
SocketTimeoutException, the socket remains open. None of its resources are released (including it’s port), meaning that you cannot bind a new Socket to that port until the Socket is closed.