简体   繁体   中英

Java I/O read Delay

When i try to read the InputStream , the reading takes a lot of time from the server . The process reads some bytes pauses for 5 mins , reads again and this way it continues. 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. 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.

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.

The process reads some bytes pauses for 5 mins

What does that mean. You pause it, or it pauses itself?

If you are pausing it for 5 minutes, don't bother. read() will block until data is available. Which might be a lot less than 5 minutes, so you are probably wasting a lot of time.

If it is pausing itself, that is because read() will block until data is available (see above). So that means the server isn't sending as fast as you expect. So investigate that, not the behaviour of the receiver.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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