How can I wake a thread that is blocked on a call to InputStream.read()?

Alex Chaffee

(...or any other IO method that wraps read(), like BufferedReader.readLine(). Calling thread.interrupt() does nothing.)

You can't. That's the way it's supposed to work.

One workaround is to use a non-blocking input stream. Such a stream would call in.available() to check for the number of bytes that can currently be read without blocking. If the number is zero, or is less than the size of the data you're expecting, then you can go on with your business in the rest of the program. One disadvantage to this technique is that you must poll for input; one big advantage to the standard block-on-read threads is that they process data as soon as it is availalble.

See Purpletech Code for source code to com.purpletech.io.NonBlockingInputStream.

See the new (Java 1.4) java.nio.channels package for non-blocking I/O support.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact