>>I need to create multiple concurrent processes. Is there a way to avoid
>>needing 2 threads per process to handle the output and error streams of the
[quoted text clipped - 17 lines]
> I've always ended up launching at least 1 extra thread to handle the
> other stream. Your current thread can handle 1 of the streams.
>>> I need to create multiple concurrent processes. Is there a way to avoid
>>> needing 2 threads per process to handle the output and error streams
[quoted text clipped - 21 lines]
>
> Why do you 'need' multiple processes (threads)?
In the general case, you need multiple threads because an external
Process' stdout and stderr must both be read in order to prevent the
process blocking. In some cases, its stdin may need to be fed
concurrently as well. There is no way to perform a non-blocking read on
a general Java InputStream, nor a non-blocking write on a general
OutputStream, so each stream that you need to worry about must be
handled in a separate thread.
I thought all this was fairly well-known to experienced Java hands. Do
you have an idea that would challenge the conventional wisdom? If so
then I would be delighted to hear it.
> Its often a design mistake to do so, most high throughput applications
> have very few threads. They tend to favour an event driven design rather
> than a multi-threaded design.
>
> (think of Swing, its single threaded and yet has to be very responsive)
I agree that minimizing the number of threads is a good plan. In the
case of a specific external process, you might be able to use knowledge
about the process to reduce the required number of threads from three to
two, or even to one. For the general case, however, I have yet to hear
how you can safely get by with fewer than three.

Signature
John Bollinger
jobollin@indiana.edu
Andrew McDonagh - 04 Jan 2006 22:58 GMT
>>>> I need to create multiple concurrent processes. Is there a way to avoid
>>>> needing 2 threads per process to handle the output and error streams
[quoted text clipped - 30 lines]
> OutputStream, so each stream that you need to worry about must be
> handled in a separate thread.
Sure, the reading of might these need different threads, but the
handling of the data that comes across them need not. The data can
usually be converted into discrete Events that are used within the
application's main thread As far as the OPs application though, we
don't know enough about it to say whether an event driven design rather
than a multi-threaded design is better or not.
> I thought all this was fairly well-known to experienced Java hands.
Hopefully it is ;-)
> Do you have an idea that would challenge the conventional wisdom? If so
> then I would be delighted to hear it.
God no - I wasn't trying to claim to have that - merely asking the OP
whether they actually need multiple threads or not.
>> Its often a design mistake to do so, most high throughput applications
>> have very few threads. They tend to favour an event driven design
[quoted text clipped - 3 lines]
>
> I agree that minimizing the number of threads is a good plan.
Yes this is the important point, which can easily be missed by
developers new to threads (I'm not implying anything about the OP - a
general statement is all).
I know from personal experience and watching other developers that we
tend to start out avoiding threads, then we over use them, then we learn
to use them wisely.
> In the case of a specific external process, you might be able to use knowledge
> about the process to reduce the required number of threads from three to
> two, or even to one. For the general case, however, I have yet to hear
> how you can safely get by with fewer than three.
Agreed, as I say, my question for the op was merely one about whether
they had need for multiple threads or whether a simpler threading model
could be an alternative.
Andrew