How to terminate a thread blocking on socket IO operation instantly?
There are (potentially) three ways to do this:
Calling
Socket.close()
on the socket will close the associatedInputStream
andOutputStream
objects, and cause any threads blocked in Socket or (associated) stream operations to be unblocked. According to the javadoc, operations on the socket itself will throw aSocketException
.Calling
Thread.interrupt()
will (under some circumstances that are not specified) interrupt a blocking I/O operation, causing it to throw anInterruptedIOException
.Note the caveat. Apparently the "interrupt()" approach doesn't work on "most" modern Java platforms. (If someone else had the time and inclination, they could possible investigate the circumstances in which this approach works. However, the mere fact that the behavior is platform specific should be sufficient to say that you should only use it if you only need your application to work on a specific platform. At which point you can easily "try it" for yourself.)
A possible third way to do this is to call
Socket.shutdownInput()
and/orSocket.shutdownOutput()
. The javadocs don't say explicitly what happens with read and/or write operations that are currently blocked, but it is not unreasonable to think that they will unblock and throw an exception. However, when the javadoc doesn't say what happens, the behavior should be assumed to be platform specific.
Interrupt/stop thread with socket I/O blocking operation
A solution, described by Peter Lawrey and also seen here is to close the socket.
With nio, You could also use a SocketChannel which is interruptible and would allow the application of the standard interrupt model of Java.
A call to the interrupt method of your Thread object would throw an InterruptedException which would stop even your blocking IO operation.
How do I shutdown a thread which contains a blocking call to NetworkStream.Read
Shutdown the socket for input. That will unblock the read and cause it to get an EOS indication, whatever form that takes in the API. As you are on Windows, it will also cause the other end to get a connection rest if it keeps sending. (This behaviour is platform-dependent.)
Kill a thread that is waiting for socket output
Close the in
stream, readLine()
should then return null
instantly.
how to kill a thread which is waiting for blocking function call in Java?
Thread.interrupt()
will not interrupt a thread blocked on a socket. You can try to call Thread.stop()
or Thread.destroy()
, but these methods are deprecated (edit: actually, absent in J2ME) and in some cases non-functional, for reasons you can read about here. As that article mentions, the best solution in your case is to close the socket that you're blocking on:
In some cases, you can use application specific tricks. For example, if a thread is waiting on a known socket, you can close the socket to cause the thread to return immediately. Unfortunately, there really isn't any technique that works in general. It should be noted that in all situations where a waiting thread doesn't respond to Thread.interrupt, it wouldn't respond to Thread.stop either. Such cases include deliberate denial-of-service attacks, and I/O operations for which thread.stop and thread.interrupt do not work properly.
How can I interrupt a ServerSocket accept() method?
You can call close()
from another thread, and the accept()
call will throw a SocketException
.
How to interupt a thread blocked by some socket IO operation without closing it
Set a shortish read timeout on the socket and loop while you get SocketTimeoutException
, checking Thread.isInterrupted()
each time. Use Thread.interrupt()
to interrupt the thread.
Related Topics
How to Convert String to Date Without Knowing the Format
How to Read the Manifest File for a Webapp Running in Apache Tomcat
Difference of Maven Jaxb Plugins
How to Set the Jdk Netbeans Runs On
System.Currenttimemillis() VS. New Date() VS. Calendar.Getinstance().Gettime()
How to Convert a Byte Array into a Double and Back
Invalid Thread Access Error with Java Swt
Variable's Scope in a Switch Case
How to Convert Binary String Value to Decimal
Measure Execution Time for a Java Method
How to Get the Desktop Path in Java
Convert Array of Strings into a String in Java
How to Call Getclass() from a Static Method in Java
Immutable VS Unmodifiable Collection
How to Negate a Method Reference Predicate
How to Make a Java Class That Implements One Interface with Two Generic Types