blob: 5aefd17f0e551cde7b36654d598e5a1d61ad8470 [file] [log] [blame]
Mathieu Chartier905f5912014-12-12 13:05:33 -08001/*
2 * Copyright (C) 2014 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.Map;
18
19public class Main implements Runnable {
20 static final int numberOfThreads = 5;
21 static final int totalOperations = 1000;
22
23 public static void main(String[] args) throws Exception {
24 final Thread[] threads = new Thread[numberOfThreads];
Mathieu Chartier4201cf02017-01-12 14:51:44 -080025 test_getStackTraces();
Mathieu Chartier905f5912014-12-12 13:05:33 -080026 for (int t = 0; t < threads.length; t++) {
27 threads[t] = new Thread(new Main());
28 threads[t].start();
29 }
30 for (Thread t : threads) {
31 t.join();
32 }
33 System.out.println("Finishing");
34 }
35
Mathieu Chartier4201cf02017-01-12 14:51:44 -080036 static void test_getStackTraces() {
37 // Check all the current threads for positive IDs.
38 Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();
39 for (Map.Entry<Thread, StackTraceElement[]> pair : map.entrySet()) {
40 Thread thread = pair.getKey();
41 // Expect empty stack trace since we do not support suspending the GC thread for
42 // obtaining stack traces. See b/28261069.
43 if (thread.getName().equals("HeapTaskDaemon")) {
44 System.out.println(thread.getName() + " depth " + pair.getValue().length);
45 }
46 }
47 }
48
Mathieu Chartier905f5912014-12-12 13:05:33 -080049 public void test_getId() {
50 if (Thread.currentThread().getId() <= 0) {
51 System.out.println("current thread's ID is not positive");
52 }
53 // Check all the current threads for positive IDs.
54 Map<Thread, StackTraceElement[]> stMap = Thread.getAllStackTraces();
55 for (Thread thread : stMap.keySet()) {
56 if (thread.getId() <= 0) {
57 System.out.println("thread's ID is not positive: " + thread.getName());
58 }
59 }
60 }
61
62 public void run() {
63 for (int i = 0; i < totalOperations; ++i) {
64 test_getId();
65 }
66 }
67}