读取套接字时的Java中断线程 [英] Java interrupt thread when reading socket
问题描述
可能重复:
如何立即终止套接字IO操作的线程阻塞?
我在线程中运行客户端想要从Java中读取套接字。但是在阅读时,也许我想要杀死这个主题。所以我中断
它,但套接字的读取方法抛出 InterruptedException
?我没有找到。
I have client run in thread want to read from socket in Java. But while reading, maybe I want to kill the thread. So I interrupt
it, but does socket's reading methods throw InterruptedException
? I didn't find.
那么,如果线程在读取套接字时阻塞,我怎能很好地让线程死掉?
So, how can I nicely ask thread to die while it's blocking on reading socket?
谢谢
推荐答案
如果你打断()一个在IO操作中被阻塞的线程,它可能会抛出 InterruptedIOException
。另一种可能解除阻塞在 Socket
上阻塞的线程的方法是关闭 Socket
。
If you interrupt() a thread that is blocked in an IO operation, it may throw InterruptedIOException
. Another possible way to unblock a thread blocked on a Socket
is to close the Socket
.
有关详细信息,请参阅我对此问题的回答:如何立即终止套接字IO操作的线程阻塞?
For more details, see my answer to this question: How to terminate a thread blocking on socket IO operation instantly?
这篇关于读取套接字时的Java中断线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!