- A Concurrent Affair - https://www.concurrentaffair.org -

Streams of Irony

As I’ve mentioned before, I seem to have problems with very long command lines, e.g. when I try to do a javac ${drjava.all.files;sep=" ";rel="${project.root}"}. With really long command lines, it seems like the project is started, and ps also shows the process, but it never does anything, nor produce output.

The fact that it never produced any output was actually good, because it made me notice that in that case, my previous implementation hangs. I was doing the reading in the AWT event thread, and if there was no output from the external process, the event thread would be blocked until output was available. I moved the actual reading from the stream into a separate thread.

When I abort a process, though, I would like to read whatever is available from the process right now, but of course I don’t want to block the event thread. I only want to read if output is available, and Java is nice enough to provide the InputStreamReader.ready() [1] method for this purpose.

The documentation of ready() states:

Returns: True if the next read() is guaranteed not to block for input, false otherwise.

That may be true, but in my case, the call to ready() itself blocks, so finding out whether the next call to read() will block is pointless.

The alternative, InputStream.available() [2] also blocks, even though the documentation states:

Returns: the number of bytes that can be read from this input stream without blocking.

Shouldn’t method which tell you if your program will block or not be written in a way so they themselves do not block? Sigh.

[3] [4]Share [5]