简体   繁体   English

Java I / O读取延迟

[英]Java I/O read Delay

When i try to read the InputStream , the reading takes a lot of time from the server . 当我尝试读取InputStream时,从服务器读取数据会花费很多时间。 The process reads some bytes pauses for 5 mins , reads again and this way it continues. 该过程读取一些字节暂停5分钟,然后再次读取,并以此方式继续。 But the thread hangs after reading some bytes . 但是线程在读取一些字节后挂起。 Is there any way to read the bytes quickly? 有什么办法可以快速读取字节?

If you are reading bytes from any device, you should expect read() to block for a period of time. 如果要从任何设备读取字节,则应该期望read()阻塞一段时间。 How long that is depends on the device. 多长时间取决于设备。 If its a Socket it depends on the other end sending the data and your network characteristics. 如果是Socket,则取决于发送数据的另一端和您的网络特征。

If you want the connection to detect a failed Socket connection, have the other end send a regular heartbeat and unless get a packet within a limited amount of time you can assume the connection is dead. 如果您希望连接检测到失败的Socket连接,请让另一端发送常规心跳,除非在有限的时间内收到数据包,否则您可以认为连接已死。

The process reads some bytes pauses for 5 mins 该过程读取一些字节暂停5分钟

What does that mean. 这意味着什么。 You pause it, or it pauses itself? 您暂停它,还是暂停它自己?

If you are pausing it for 5 minutes, don't bother. 如果您暂停了5分钟,请不要打扰。 read() will block until data is available. read()将阻塞,直到有可用数据为止。 Which might be a lot less than 5 minutes, so you are probably wasting a lot of time. 可能不到5分钟,所以您可能浪费了很多时间。

If it is pausing itself, that is because read() will block until data is available (see above). 如果它本身正在暂停,那是因为read()将阻塞直到有可用数据为止(请参见上文)。 So that means the server isn't sending as fast as you expect. 因此,这意味着服务器的发送速度没有达到您的预期。 So investigate that, not the behaviour of the receiver. 因此,请调查该消息,而不是接收者的行为。

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

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