blob: 3d7a4ca7407c40d998607af42ed90f6209b3eecb [file] [log] [blame]
Andreas Gamped18d9e22017-01-16 16:08:45 +00001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import java.util.Arrays;
18import java.util.Comparator;
19
20public class Main {
21 public static void main(String[] args) throws Exception {
Andreas Gamped18d9e22017-01-16 16:08:45 +000022 doTest();
23 }
24
25 private static void doTest() throws Exception {
26 Thread t1 = Thread.currentThread();
27 ThreadGroup curGroup = t1.getThreadGroup();
28
29 ThreadGroup rootGroup = curGroup;
30 while (rootGroup.getParent() != null) {
31 rootGroup = rootGroup.getParent();
32 }
33
34 ThreadGroup topGroups[] = getTopThreadGroups();
35 if (topGroups == null || topGroups.length != 1 || topGroups[0] != rootGroup) {
36 System.out.println(Arrays.toString(topGroups));
37 throw new RuntimeException("Unexpected topGroups");
38 }
39
40 printThreadGroupInfo(curGroup);
41 printThreadGroupInfo(rootGroup);
42
43 waitGroupChildren(rootGroup, 5 /* # daemons */, 30 /* timeout in seconds */);
44
45 checkChildren(curGroup);
46 }
47
48 private static void printThreadGroupInfo(ThreadGroup tg) {
49 Object[] threadGroupInfo = getThreadGroupInfo(tg);
50 if (threadGroupInfo == null || threadGroupInfo.length != 4) {
51 System.out.println(Arrays.toString(threadGroupInfo));
52 throw new RuntimeException("threadGroupInfo length wrong");
53 }
54
55 System.out.println(tg);
56 System.out.println(" " + threadGroupInfo[0]); // Parent
57 System.out.println(" " + threadGroupInfo[1]); // Name
58 System.out.println(" " + threadGroupInfo[2]); // Priority
59 System.out.println(" " + threadGroupInfo[3]); // Daemon
60 }
61
62 private static void checkChildren(ThreadGroup tg) {
63 Object[] data = getThreadGroupChildren(tg);
64 Thread[] threads = (Thread[])data[0];
65 ThreadGroup[] groups = (ThreadGroup[])data[1];
66
67 Arrays.sort(threads, THREAD_COMP);
68 Arrays.sort(groups, THREADGROUP_COMP);
69 System.out.println(tg.getName() + ":");
70 System.out.println(" " + Arrays.toString(threads));
71 System.out.println(" " + Arrays.toString(groups));
72
73 if (tg.getParent() != null) {
74 checkChildren(tg.getParent());
75 }
76 }
77
78 private static void waitGroupChildren(ThreadGroup tg, int expectedChildCount, int timeoutS)
79 throws Exception {
80 for (int i = 0; i < timeoutS; i++) {
81 Object[] data = getThreadGroupChildren(tg);
82 Thread[] threads = (Thread[])data[0];
83 if (threads.length == expectedChildCount) {
84 return;
85 }
86 Thread.sleep(1000);
87 }
88
89 Object[] data = getThreadGroupChildren(tg);
90 Thread[] threads = (Thread[])data[0];
91 System.out.println(Arrays.toString(threads));
92 throw new RuntimeException("Waited unsuccessfully for " + expectedChildCount + " children.");
93 }
94
95 private final static Comparator<Thread> THREAD_COMP = new Comparator<Thread>() {
96 public int compare(Thread o1, Thread o2) {
97 return o1.getName().compareTo(o2.getName());
98 }
99 };
100
101 private final static Comparator<ThreadGroup> THREADGROUP_COMP = new Comparator<ThreadGroup>() {
102 public int compare(ThreadGroup o1, ThreadGroup o2) {
103 return o1.getName().compareTo(o2.getName());
104 }
105 };
106
107 private static native ThreadGroup[] getTopThreadGroups();
108 private static native Object[] getThreadGroupInfo(ThreadGroup tg);
109 // Returns an array where element 0 is an array of threads and element 1 is an array of groups.
110 private static native Object[] getThreadGroupChildren(ThreadGroup tg);
111}