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("/system/bin/sleep", "5"); 18 Process proc = pb.start(); 19 checkManager(); 20 proc.waitFor(); 21 System.out.println("child died"); 22 } 23 checkManager()24 static private void checkManager() { 25 Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces(); 26 boolean found = false; 27 28 for (Map.Entry<Thread, StackTraceElement[]> entry : 29 traces.entrySet()) { 30 Thread t = entry.getKey(); 31 String name = t.getName(); 32 if (name.equals("java.lang.ProcessManager")) { 33 System.out.println("process manager: " + t.getState()); 34 found = true; 35 } 36 } 37 38 if (! found) { 39 System.out.println("process manager: nonexistent"); 40 } 41 } 42 } 43