Andreas Gampe | 966de9e | 2017-01-12 20:51:02 -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 | import java.util.ArrayList; |
| 18 | import java.util.List; |
| 19 | |
| 20 | public class AllTraces { |
| 21 | private final static List<Object> RETAIN = new ArrayList<Object>(); |
| 22 | |
| 23 | public static void doTest() throws Exception { |
| 24 | System.out.println("################################"); |
| 25 | System.out.println("### Other threads (suspended) ###"); |
| 26 | System.out.println("################################"); |
| 27 | |
| 28 | // Also create an unstarted and a dead thread. |
Andreas Gampe | 8a3d0b8 | 2017-02-14 14:28:32 -0800 | [diff] [blame] | 29 | RETAIN.add(new Thread("UNSTARTED")); |
| 30 | Thread deadThread = new Thread("DEAD"); |
Andreas Gampe | 966de9e | 2017-01-12 20:51:02 -0800 | [diff] [blame] | 31 | RETAIN.add(deadThread); |
| 32 | deadThread.start(); |
| 33 | deadThread.join(); |
| 34 | |
| 35 | final int N = 10; |
| 36 | |
| 37 | final ControlData data = new ControlData(N); |
| 38 | data.waitFor = new Object(); |
| 39 | |
| 40 | Thread threads[] = new Thread[N]; |
| 41 | |
| 42 | for (int i = 0; i < N; i++) { |
Andreas Gampe | 8a3d0b8 | 2017-02-14 14:28:32 -0800 | [diff] [blame] | 43 | Thread t = new Thread("AllTraces Thread " + i) { |
Andreas Gampe | 966de9e | 2017-01-12 20:51:02 -0800 | [diff] [blame] | 44 | public void run() { |
| 45 | Recurse.foo(4, 0, 0, data); |
| 46 | } |
| 47 | }; |
| 48 | t.start(); |
| 49 | threads[i] = t; |
| 50 | } |
| 51 | data.reached.await(); |
| 52 | Thread.yield(); |
| 53 | Thread.sleep(500); // A little bit of time... |
| 54 | |
| 55 | printAll(0); |
| 56 | |
| 57 | printAll(5); |
| 58 | |
| 59 | printAll(25); |
| 60 | |
| 61 | // Let the thread make progress and die. |
| 62 | synchronized(data.waitFor) { |
| 63 | data.waitFor.notifyAll(); |
| 64 | } |
| 65 | for (int i = 0; i < N; i++) { |
| 66 | threads[i].join(); |
| 67 | } |
| 68 | |
| 69 | RETAIN.clear(); |
| 70 | } |
| 71 | |
| 72 | public static void printAll(int max) { |
| 73 | PrintThread.printAll(getAllStackTraces(max)); |
| 74 | } |
| 75 | |
| 76 | // Get all stack traces. This will return an array with an element for each thread. The element |
| 77 | // is an array itself with the first element being the thread, and the second element a nested |
| 78 | // String array as in getStackTrace. |
| 79 | public static native Object[][] getAllStackTraces(int max); |
| 80 | } |