Any way of using java.nio.* to interrupt a InputStream#read() without closing socket?

本秂侑毒 提交于 2019-12-01 03:30:27

If you interrupt an InterruptibleChannel during a read, it will be closed and throw a ClosedByInterruptException. If you just want a read timeout, don't use a channel at all, just a regular Socket; call Socket.setSoTimeout() with a shortish timeout, and check the isInterrupted() status of the thread every time the timeout triggers. Better still, review your requirement to interrupt the thread. What's that for?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!