summaryrefslogtreecommitdiff
path: root/tests/063-process-manager/src/Main.java
blob: c94b8adc5e717dd7cff49863e3890cd22a25167c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.Map;

public class Main {
    static public void main(String[] args) throws Exception {
        checkManager();
        for (int i = 1; i <= 2; i++) {
            System.out.println("\nspawning child #" + i);
            child();
            Thread.sleep(2000);
            checkManager();
        }
        System.out.println("\ndone!");
    }

    static private void child() throws Exception {
        System.out.println("spawning child");
        ProcessBuilder pb = new ProcessBuilder("/system/bin/sleep", "5");
        Process proc = pb.start();
        Thread.sleep(1000);
        checkManager();
        proc.waitFor();
        System.out.println("child died");
    }

    static private void checkManager() {
        Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
        boolean found = false;

        for (Map.Entry<Thread, StackTraceElement[]> entry :
                 traces.entrySet()) {
            Thread t = entry.getKey();
            String name = t.getName();
            if (name.equals("java.lang.ProcessManager")) {
                System.out.println("process manager: " + t.getState());
                found = true;
            }
        }

        if (! found) {
            System.out.println("process manager: nonexistent");
        }
    }
}