1 import java.util.Map; 2 3 public class Main { main(String[] args)4 static public void main(String[] args) throws Exception { 5 checkManager(); 6 for (int i = 1; i <= 2; i++) { 7 System.out.println("\nspawning child #" + i); 8 child(); 9 Thread.sleep(2000); 10 checkManager(); 11 } 12 System.out.println("\ndone!"); 13 } 14 child()15 static private void child() throws Exception { 16 System.out.println("spawning child"); 17 ProcessBuilder pb = new ProcessBuilder("sleep", "5"); 18 Process proc = pb.start(); 19 Thread.sleep(1000); 20 checkManager(); 21 proc.waitFor(); 22 System.out.println("child died"); 23 } 24 checkManager()25 static private void checkManager() { 26 Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces(); 27 boolean found = false; 28 29 for (Map.Entry<Thread, StackTraceElement[]> entry : 30 traces.entrySet()) { 31 Thread t = entry.getKey(); 32 String name = t.getName(); 33 if (name.equals("java.lang.ProcessManager")) { 34 System.out.println("process manager: " + t.getState()); 35 found = true; 36 } 37 } 38 39 if (! found) { 40 System.out.println("process manager: nonexistent"); 41 } 42 } 43 } 44