blocking IO

P

Peter Ashford

For a thread blocking on InputStream.read() can I interrupt it
successfully and resume reading after handling the interruption, or
would it be better to use NIO socket channels and avoid blocking
altogether using asynchronous IO?
 
J

John C. Bollinger

Peter said:
For a thread blocking on InputStream.read() can I interrupt it
successfully and resume reading after handling the interruption, or
would it be better to use NIO socket channels and avoid blocking
altogether using asynchronous IO?

InputStream.read() blocks until it reads something, detects end of
stream, or receives an exception. Unless you can manage to cause the
read to receive an I/O exception you cannot interrupt it without
forcefully stopping the thread in which it runs (not recommended). I
wouldn't count on being able to resume reading after such a stoppage anyway.

If you anticipate being in a position to interrupt the read in the first
place then you have a multithreaded design in mind. Why not just
perform the needed action in a different thread than the one blocked on
the read? Non-blocking I/O through NIO is another alternative; which
(if either) is better depends on your application.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top