blob: 1d9aa96d6e3008a1cb7a8fbf670041ac09af4cc5 [file] [log] [blame]
Andreas Gampe966de9e2017-01-12 20:51:02 -08001/*
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
17import java.util.ArrayList;
18import java.util.List;
19
20public 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 Gampe8a3d0b82017-02-14 14:28:32 -080029 RETAIN.add(new Thread("UNSTARTED"));
30 Thread deadThread = new Thread("DEAD");
Andreas Gampe966de9e2017-01-12 20:51:02 -080031 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 Gampe8a3d0b82017-02-14 14:28:32 -080043 Thread t = new Thread("AllTraces Thread " + i) {
Andreas Gampe966de9e2017-01-12 20:51:02 -080044 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}