少なくとも stdout と stderr を食べないと、最終的にメモリ不足になることがわかりました。また、複数のプロセスを同時に実行することもできませんでした。
これを行うために、ProcessStreamEater という名前のクラスを使用しています。
public class ProcessStreamEater implements Runnable
{
private final Process proc;
public ProcessStreamEater(Process proc)
{
this.proc = proc;
}
@Override
public void run()
{
InputStreamReader r = new InputStreamReader(proc.getInputStream());
try
{
while(r.read() != -1)
{ // put stuff here if you want to do something with output
// otherwise, empty
}
}
catch(IOException e)
{
// handle IO exception
}
finally
{
if(r != null)
{
try
{
r.close();
}
catch(IOException c)
{}
}
}
}
}
それを使って物を食べると…
ProcessBuilder pb = new ProcessBuilder(args);
pb.redirectErrorStream(true);
final Process proc = pb.start();
executorService.execute(new ProcessStreamEater(proc));
executorService がExecutors.newCachedThreadPool ()で作成された場所