简体   繁体   English

如何立即终止套接字IO操作的线程阻塞?

[英]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. 在Java的上下文中,我在打开GUI窗口时创建了一个新线程来读取网络输入,当我关闭窗口时,我想释放套接字资源并立即终止线程。 Now i am using setSoTimeout method, but i don't want to wait the timeout exception. 现在我使用setSoTimeout方法,但我不想等待超时异常。 Could anybody give some suggestion? 有人可以提一些建议吗? Thanks! 谢谢!

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. 在套接字上调用Socket.close()将关闭关联的InputStreamOutputStream对象,并导致在Socket或(关联)流操作中阻塞的任何线程被解除阻塞。 According to the javadoc, operations on the socket itself will throw a SocketException . 根据javadoc,对套接字本身的操作将抛出SocketException

  • Calling Thread.interrupt() will (under some circumstances that are not specified) interrupt a blocking I/O operation, causing it to throw an InterruptedIOException . 调用Thread.interrupt()将(在某些情况下未指定)中断阻塞I / O操作,导致它抛出InterruptedIOException

    Note the caveat. 请注意警告。 Apparently the "interrupt()" approach doesn't work on "most" modern Java platforms. 显然,“interrupt()”方法不适用于“大多数”现代Java平台。 (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() . 可能的第三种方法是调用Socket.shutdownInput()和/或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. javadocs没有明确说明当前被阻止的读取和/或写入操作会发生什么,但认为它们将解除阻塞并抛出异常并不是不合理的。 However, if the javadoc doesn't say what happens then the behavior should be assumed to be platform specific. 但是,如果javadoc没有说明会发生什么,那么应该假定该行为是特定于平台的。

I know this question is old but as nobody seems to have solved the "mystery" of Thread.interrupt() on "modern platforms" I did some research. 我知道这个问题已经过时了,但似乎没有人在“现代平台”上解决了Thread.interrupt()的“神秘面纱”,我做了一些研究。

This is tested on Java 8 on Windows7 (64-bit) (but it will possibly be true for other platforms as well). 这是在Windows7(64位)上的Java 8上测试的(但对于其他平台也可能如此)。

Calling Thread.interrupt() does not throw an InterruptedIOException What happens is that the InputStream.read() method returns with -1 and Thread.interrupted() -flag is set. 调用Thread.interrupt() 不会抛出InterruptedIOException会发生什么情况是, InputStream.read()方法以返回-1Thread.interrupted() -flag设置。

So the following could be considered a 'corrected' read() throwing InterruptedIOException : 所以以下内容可以被认为是'纠正'read()抛出InterruptedIOException

static final int read(Socket socket, byte[] inData) 
    throws SocketTimeoutException, // if setSoTimeout() was set and read timed out
           InterruptedIOException, // if thread interrupted
           IOException             // other erors
{
    InputStream in = socket.getInputStream();

    int readBytes = in.read( inData, 0, inData.length);

    if  ( Thread.interrupted() )
    {
        throw new InterruptedIOException( "Thread interrupted during socket read");
    }

    return readBytes;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何终止套接字连接上的线程阻塞 - How to terminate a thread blocking on socket connect 如何终止特定的阻塞线程 - How to terminate a particular blocking thread 反应式邮件程序 - IO 线程上的阻塞操作 - Reactive Mailer - blocking operation on a IO thread 如何在没有关闭的情况下中断被某个套接字IO操作阻塞的线程 - How to interupt a thread blocked by some socket IO operation without closing it 其他线程中的IO操作阻塞了我的UI线程? - IO operation in other thread blocking my UI thread? quarkus: IllegalStateException: 您试图在 IO 线程上执行阻塞操作。 这是不允许的,因为阻塞了 IO 线程 - quarkus: IllegalStateException: You have attempted to perform a blocking operation on a IO thread. This is not allowed, as blocking the IO thread BlockingOperationNotAllowedException - 您试图在 IO 线程上执行阻塞操作 - BlockingOperationNotAllowedException - You have attempted to perform a blocking operation on a IO thread 如何在超时时终止服务器套接字线程? - How to terminate a server socket thread on timeout? 如何为 Webflux 应用程序包装阻塞 IO 操作 - How to wrap blocking IO operation for Webflux applicaiton 具有套接字I / O阻塞操作的中断/停止线程 - Interrupt/stop thread with socket I/O blocking operation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM