1 package org.feistymeow.process;
4 //Name : ThreadSpawnerAndWatcher
5 //Author : Chris Koeritz
6 //Rights : Copyright (c) 2012-$now By University of Virginia
8 //This file is free software; you can modify/redistribute it under the terms
9 //of the Apache License v2.0: http://www.apache.org/licenses/LICENSE-2.0
10 //Feel free to send updates to: [ koeritz@virginia.edu ]
14 // test app by Chris Koeritz.
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;
30 // will be spawned multiply and each one will check its one process.
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);
49 * Launches the thread and waits for its results.
53 if (null == this.myThread) {
54 this.myThread = new Thread(this);
55 this.myThread.start();
60 * Stops the execution of the ProcessWatchingThread - or tries to.
64 Thread goAway = myThread;
72 * Returns true if the thread isn't null, i.e. it is still running.
74 public boolean threadRunning()
76 return (null != this.myThread);
81 if (false == threadRunning()) {
82 return; // stopped before it ever started
86 // c_logger.info("about to start process: " + cmdline);
87 Process p = procbuild.start();
89 // c_logger.info("returned from awaiting process: " + cmdline);
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);
95 this.myThread = null; // thread is exiting
99 static public void main(String[] args) throws Throwable
101 PropertyConfigurator.configure("log4j.properties"); // hard-coded for
102 // eclipse based run.
104 Vector<String> cmds = new Vector<String>();
106 cmds.add("/bin/echo");
107 cmds.add("hello jupiter");
109 // cmds.add("/bin/sleep");
112 Vector<ThreadSpawnerAndWatcher> watchers = new Vector<ThreadSpawnerAndWatcher>();
114 c_logger.info("revving up the process launching test.");
116 // create all the threads and get them ready to go.
117 for (int i = 0; i < Defaults.CONCURRENT_LAUNCHES; i++) {
118 ThreadSpawnerAndWatcher newby = new ThreadSpawnerAndWatcher(cmds);
122 // randomize start order?
123 // now start all the threads, which will cause all our process launches.
124 for (int i = 0; i < Defaults.CONCURRENT_LAUNCHES; i++) {
125 ThreadSpawnerAndWatcher curr = watchers.get(i);
129 // now wait for them all to finish. if we never get out, then there's a bug
131 for (int i = 0; i < Defaults.CONCURRENT_LAUNCHES; i++) {
132 ThreadSpawnerAndWatcher curr = watchers.get(i);
133 while (curr.threadRunning()) {
136 } catch (InterruptedException e) {
142 // currently test will never come back out to finish, if there's a failure
144 c_logger.info("Test Succeeded: all spawned processes came back as expected.");