Sub-process in java are very expensive. Each process is usually support by a NUMBERS of threads.
- a thread to host the process (by JDK 1.6 on linux)
- a thread to read to read/print/ignore the input stream
- another thread to read/print/ignore the error stream
- a more thread to do timeout and monitoring and kill sub-process by your application
- the business logic thread, holduntil for the sub-process return.
The number of thread get out of control if you have a pool of thread focking sub-process to do tasks. As a result, there may be more then a double of concurrent thread at peak.
In many cases, we fork a process just because nobody able to write JNI to call native function missing from the JDK (e.g. chmod, ln, ls), trigger a shell script, etc, etc.
Some thread can be saved, but some thread should run to prevent the worst case (buffer overrun on inputstream).
How can I reduce the overhead of creating sub-process in Java to the minimum? I am thinking of NIO the stream handles, combine and share threads, lower background thread priority, re-use of process. But I have no idea are they possible or not.