import java.io.*; import java.util.concurrent.CountDownLatch; public class EndlessLoop { public static void main(final String[] args) throws InterruptedException { if (args.length == 0) { int cnt = 0; while (true) { System.err.print("."); Thread.sleep(2000); if (++cnt > 80) { cnt = 0; System.err.println(); } } } else { int threadCount = Integer.parseInt(args[0]); final CountDownLatch latch = new CountDownLatch(threadCount); for (int i = 0; i < threadCount; i++) { final int id = i; new Thread() { @Override public void run() { latch.countDown(); try { latch.await(); System.out.println("running cmd: '" + args[1] + "'"); Process process = Runtime.getRuntime().exec(args[1]); FileWriter out = new FileWriter("out"+id+".txt"); InputStream pstream; int read; pstream = process.getErrorStream(); while ((read = pstream.read()) != -1){ out.write(read); } pstream.close(); out.close(); process.waitFor(); System.out.println("Process " + id + ": " + process.exitValue()); } catch (Exception e) { e.printStackTrace(); } } }.start(); } } } }