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