blob: 2cfc0d7fc22369950c10c6ec314abf0a0a323863 (
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
import java.util.Map;
public class Main {
static public void main(String[] args) throws Exception {
checkManager();
// Warm up the reaper so that there are no issues with scheduling because of static
// initialization.
{
ProcessBuilder pb = new ProcessBuilder("sleep", "0");
Process proc = pb.start();
proc.waitFor();
waitForReaperTimedWaiting(true /* reaperMustExist */);
}
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("sleep", "5");
Process proc = pb.start();
Thread.sleep(250);
checkManager();
proc.waitFor();
System.out.println("child died");
}
private static boolean isReaperThread(Thread t) {
String name = t.getName();
return name.indexOf("process reaper") >= 0;
}
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();
if (isReaperThread(t)) {
Thread.State state = t.getState();
System.out.println("process manager: " + state);
if (state != Thread.State.RUNNABLE && state != Thread.State.TIMED_WAITING) {
for (StackTraceElement e : entry.getValue()) {
System.out.println(" " + e);
}
}
found = true;
}
}
if (! found) {
System.out.println("process manager: nonexistent");
}
}
private static void waitForReaperTimedWaiting(boolean reaperMustExist) {
for (;;) {
Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
boolean ok = true;
boolean found = false;
for (Thread t : traces.keySet()) {
if (isReaperThread(t)) {
found = true;
Thread.State state = t.getState();
if (state != Thread.State.TIMED_WAITING) {
ok = false;
break;
}
}
}
if (ok && (!reaperMustExist || found)) {
return;
}
try {
Thread.sleep(100);
} catch (Exception e) {
// Ignore.
}
}
}
}
|