开发者

Can I set a timeout for a InputStream's read() function?

开发者 https://www.devze.com 2022-12-27 01:50 出处:网络
I have a DataInputStream that I obtained from a Socket. Is there any way I can set a timeout for dis.read(...)? Currently I spawn a new thread to do the read. While the parent thread does a thread.joi

I have a DataInputStream that I obtained from a Socket. Is there any way I can set a timeout for dis.read(...)? Currently I spawn a new thread to do the read. While the parent thread does a thread.join(timeout) to wait before interrupting it. I am aware of nio, but I don't think I want to refactor that much at this p开发者_Go百科oint. Thanks.


Not on the InputStream generally, but you can use Socket#setSoTimeout(int) to set a timeout for all read operations on the socket itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消