Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | public class ThreadListTraces { |
| 18 | public static void doTest() throws Exception { |
| 19 | System.out.println("########################################"); |
| 20 | System.out.println("### Other select threads (suspended) ###"); |
| 21 | System.out.println("########################################"); |
| 22 | |
| 23 | final int N = 10; |
| 24 | |
| 25 | final ControlData data = new ControlData(N); |
| 26 | data.waitFor = new Object(); |
| 27 | |
| 28 | Thread threads[] = new Thread[N]; |
| 29 | |
| 30 | Thread list[] = new Thread[N/2 + 1]; |
| 31 | |
| 32 | for (int i = 0; i < N; i++) { |
| 33 | Thread t = new Thread() { |
| 34 | public void run() { |
| 35 | Recurse.foo(4, 0, 0, data); |
| 36 | } |
| 37 | }; |
| 38 | t.start(); |
| 39 | threads[i] = t; |
| 40 | if (i % 2 == 0) { |
| 41 | list[i/2] = t; |
| 42 | } |
| 43 | } |
| 44 | list[list.length - 1] = Thread.currentThread(); |
| 45 | |
| 46 | data.reached.await(); |
| 47 | Thread.yield(); |
| 48 | Thread.sleep(500); // A little bit of time... |
| 49 | |
| 50 | printList(list, 0); |
| 51 | |
| 52 | printList(list, 5); |
| 53 | |
| 54 | printList(list, 25); |
| 55 | |
| 56 | // Let the thread make progress and die. |
| 57 | synchronized(data.waitFor) { |
| 58 | data.waitFor.notifyAll(); |
| 59 | } |
| 60 | for (int i = 0; i < N; i++) { |
| 61 | threads[i].join(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | public static void printList(Thread[] threads, int max) { |
| 66 | PrintThread.printAll(getThreadListStackTraces(threads, max)); |
| 67 | } |
| 68 | |
| 69 | // Similar to getAllStackTraces, but restricted to the given threads. |
| 70 | public static native Object[][] getThreadListStackTraces(Thread threads[], int max); |
| 71 | } |