简体   繁体   English

我可以为InputStream的read()函数设置超时吗?

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

I have a DataInputStream that I obtained from a Socket . 我有一个从Socket获得的DataInputStream Is there any way I can set a timeout for dis.read(...) ? 有什么办法可以为dis.read(...)设置超时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. 而父线程在中断之前执行thread.join(timeout)等待。 I am aware of nio, but I don't think I want to refactor that much at this point. 我知道nio,但我认为我不想在这一点上重构那么多。 Thanks. 谢谢。

通常不在InputStream上,但您可以使用Socket#setSoTimeout(int)为套接字本身上的所有读取操作设置超时。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM