我可以为InputStream的read()函数设置超时吗? [英] Can I set a timeout for a InputStream's read() function?

查看:1102
本文介绍了我可以为InputStream的read()函数设置超时吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 Socket 获得了 DataInputStream 。有什么办法可以为 dis.read(...)设置超时?目前我产生了一个新线程来进行读取。虽然父线程执行 thread.join(超时)在中断之前等待。我知道nio,但我认为我不想在这一点上重构那么多。谢谢。

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 point. Thanks.

推荐答案

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

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

这篇关于我可以为InputStream的read()函数设置超时吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆