Andreas Gampe | 02afcde | 2017-01-12 17:34:39 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | import java.util.Arrays; |
| 18 | import java.util.Comparator; |
| 19 | |
| 20 | public class Main { |
| 21 | public static void main(String[] args) throws Exception { |
| 22 | System.loadLibrary(args[1]); |
| 23 | |
| 24 | doTest(); |
| 25 | } |
| 26 | |
| 27 | private static void doTest() throws Exception { |
| 28 | Thread t1 = Thread.currentThread(); |
| 29 | ThreadGroup curGroup = t1.getThreadGroup(); |
| 30 | |
| 31 | ThreadGroup rootGroup = curGroup; |
| 32 | while (rootGroup.getParent() != null) { |
| 33 | rootGroup = rootGroup.getParent(); |
| 34 | } |
| 35 | |
| 36 | ThreadGroup topGroups[] = getTopThreadGroups(); |
| 37 | if (topGroups == null || topGroups.length != 1 || topGroups[0] != rootGroup) { |
| 38 | System.out.println(Arrays.toString(topGroups)); |
| 39 | throw new RuntimeException("Unexpected topGroups"); |
| 40 | } |
| 41 | |
| 42 | printThreadGroupInfo(curGroup); |
| 43 | printThreadGroupInfo(rootGroup); |
| 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 final static Comparator<Thread> THREAD_COMP = new Comparator<Thread>() { |
| 79 | public int compare(Thread o1, Thread o2) { |
| 80 | return o1.getName().compareTo(o2.getName()); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | private final static Comparator<ThreadGroup> THREADGROUP_COMP = new Comparator<ThreadGroup>() { |
| 85 | public int compare(ThreadGroup o1, ThreadGroup o2) { |
| 86 | return o1.getName().compareTo(o2.getName()); |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | private static native ThreadGroup[] getTopThreadGroups(); |
| 91 | private static native Object[] getThreadGroupInfo(ThreadGroup tg); |
| 92 | // Returns an array where element 0 is an array of threads and element 1 is an array of groups. |
| 93 | private static native Object[] getThreadGroupChildren(ThreadGroup tg); |
| 94 | } |