Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to terminate a thread blocking on socket IO operation instantly?

In the context of Java, I create a new thread to read network input when open a GUI window, and when i close the window, i want to release the socket resource and terminate the thread immediately. Now i am using setSoTimeout method, but i don't want to wait the timeout exception. Could anybody give some suggestion? Thanks!

like image 628
dingx Avatar asked Dec 13 '10 02:12

dingx


People also ask

Can you interrupt a blocked thread?

A blocked thread can be interrupted by calling the interrupt() method of Thread class. This interrupt is a pure Java mechanism and is neither CPU nor operating system level interrupt. The interrupt() method does not interrupt a running thread i.e. thread which is in RUNNABLE state.

Why do threads block on I O in Java?

Thread gets blocked just because when two thread try to access the single resource simultaneously the threads get blocked and when one thread is using the resource other should be put to sleep state because when the thread which is in processing produces the resullt only then the resource is allowed to another thread.

What is blocking of thread?

A Blocked state will occur whenever a thread tries to acquire lock on object and some other thread is already holding the lock. Once other threads have left and its this thread chance, it moves to Runnable state after that it is eligible pick up work based on JVM threading mechanism and moves to run state.

Why are threads blocked?

A thread will enter into a BLOCKED state when it couldn't acquire a lock on an object because another thread already holds the lock on the same object and doesn't release it.


1 Answers

There are (potentially) three ways to do this:

  • Calling Socket.close() on the socket will close the associated InputStream and OutputStream 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 a SocketException.

  • Calling Thread.interrupt() will (under some circumstances that are not specified) interrupt a blocking I/O operation, causing it to throw an InterruptedIOException.

    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/or Socket.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, if the javadoc doesn't say what happens then the behavior should be assumed to be platform specific.

like image 173
Stephen C Avatar answered Oct 21 '22 20:10

Stephen C