1 package org.feistymeow.process;
16 import java.io.IOException;
17 import java.lang.ProcessBuilder;
18 import java.lang.Process;
19 import java.util.Vector;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.log4j.PropertyConfigurator;
27 public static int CONCURRENT_LAUNCHES = 1000;
31 class ThreadSpawnerAndWatcher
implements Runnable
33 private static Log c_logger = LogFactory.getLog(ThreadSpawnerAndWatcher.class);
35 private volatile Thread myThread;
37 private Vector<String> cmdline;
39 private ProcessBuilder procbuild;
41 @SuppressWarnings(
"unchecked")
42 public ThreadSpawnerAndWatcher(Vector<String> command_line)
44 cmdline = (Vector<String>) command_line.clone();
45 procbuild =
new ProcessBuilder(cmdline);
53 if (
null == this.myThread) {
54 this.myThread =
new Thread(
this);
55 this.myThread.start();
64 Thread goAway = myThread;
74 public boolean threadRunning()
76 return (
null != this.myThread);
81 if (
false == threadRunning()) {
87 Process p = procbuild.start();
90 }
catch (IOException e) {
91 c_logger.debug(
"thread caught io exception on: " + cmdline);
92 }
catch (InterruptedException ie) {
93 c_logger.debug(
"thread interrupted for: " + cmdline);
99 static public void main(String[] args)
throws Throwable
101 PropertyConfigurator.configure(
"log4j.properties");
104 Vector<String> cmds =
new Vector<String>();
106 cmds.add(
"/bin/echo");
107 cmds.add(
"hello jupiter");
112 Vector<ThreadSpawnerAndWatcher> watchers =
new Vector<ThreadSpawnerAndWatcher>();
114 c_logger.info(
"revving up the process launching test.");
117 for (
int i = 0; i < Defaults.CONCURRENT_LAUNCHES; i++) {
118 ThreadSpawnerAndWatcher newby =
new ThreadSpawnerAndWatcher(cmds);
124 for (
int i = 0; i < Defaults.CONCURRENT_LAUNCHES; i++) {
125 ThreadSpawnerAndWatcher curr = watchers.get(i);
131 for (
int i = 0; i < Defaults.CONCURRENT_LAUNCHES; i++) {
132 ThreadSpawnerAndWatcher curr = watchers.get(i);
133 while (curr.threadRunning()) {
136 }
catch (InterruptedException e) {
144 c_logger.info(
"Test Succeeded: all spawned processes came back as expected.");