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