blob: 492a7ac6d17321cf06a67328a1b66558d4def83b [file] [log] [blame]
Andreas Gampeaf13ab92017-01-11 20:57:40 -08001/*
2 * Copyright (C) 2017 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.Arrays;
Andreas Gampe72c19832017-01-12 13:22:16 -080018import java.util.ArrayList;
19import java.util.Collections;
Andreas Gampe85807442017-01-13 14:40:58 -080020import java.util.Comparator;
Andreas Gampe72c19832017-01-12 13:22:16 -080021import java.util.concurrent.CountDownLatch;
22import java.util.HashMap;
23import java.util.List;
24import java.util.Map;
Andreas Gampeaf13ab92017-01-11 20:57:40 -080025
26public class Main {
27 public static void main(String[] args) throws Exception {
28 System.loadLibrary(args[1]);
29
30 doTest();
31 }
32
33 private static void doTest() throws Exception {
34 Thread t1 = Thread.currentThread();
35 Thread t2 = getCurrentThread();
36
37 if (t1 != t2) {
38 throw new RuntimeException("Expected " + t1 + " but got " + t2);
39 }
40 System.out.println("currentThread OK");
41
42 printThreadInfo(t1);
43 printThreadInfo(null);
44
45 Thread t3 = new Thread("Daemon Thread");
46 t3.setDaemon(true);
47 // Do not start this thread, yet.
48 printThreadInfo(t3);
49 // Start, and wait for it to die.
50 t3.start();
51 t3.join();
Andreas Gampe72c19832017-01-12 13:22:16 -080052 Thread.sleep(500); // Wait a little bit.
Andreas Gampeaf13ab92017-01-11 20:57:40 -080053 // Thread has died, check that we can still get info.
54 printThreadInfo(t3);
Andreas Gampe72c19832017-01-12 13:22:16 -080055
56 doStateTests();
Andreas Gampe85807442017-01-13 14:40:58 -080057
58 doAllThreadsTests();
Andreas Gampe72c19832017-01-12 13:22:16 -080059 }
60
61 private static class Holder {
62 volatile boolean flag = false;
63 }
64
65 private static void doStateTests() throws Exception {
66 System.out.println(Integer.toHexString(getThreadState(null)));
67 System.out.println(Integer.toHexString(getThreadState(Thread.currentThread())));
68
69 final CountDownLatch cdl1 = new CountDownLatch(1);
70 final CountDownLatch cdl2 = new CountDownLatch(1);
71 final CountDownLatch cdl3_1 = new CountDownLatch(1);
72 final CountDownLatch cdl3_2 = new CountDownLatch(1);
73 final CountDownLatch cdl4 = new CountDownLatch(1);
74 final CountDownLatch cdl5 = new CountDownLatch(1);
75 final Holder h = new Holder();
76 Runnable r = new Runnable() {
77 @Override
78 public void run() {
79 try {
80 cdl1.countDown();
81 synchronized(cdl1) {
82 cdl1.wait();
83 }
84
85 cdl2.countDown();
86 synchronized(cdl2) {
87 cdl2.wait(1000); // Wait a second.
88 }
89
90 cdl3_1.await();
91 cdl3_2.countDown();
92 synchronized(cdl3_2) {
93 // Nothing, just wanted to block on cdl3.
94 }
95
96 cdl4.countDown();
97 Thread.sleep(1000);
98
99 cdl5.countDown();
100 while (!h.flag) {
101 // Busy-loop.
102 }
103 } catch (Exception e) {
104 throw new RuntimeException(e);
105 }
106 }
107 };
108
109 Thread t = new Thread(r);
110 printThreadState(t);
111 t.start();
112
113 // Waiting.
114 cdl1.await();
115 Thread.yield();
116 Thread.sleep(100);
117 printThreadState(t);
118 synchronized(cdl1) {
119 cdl1.notifyAll();
120 }
121
122 // Timed waiting.
123 cdl2.await();
124 Thread.yield();
125 Thread.sleep(100);
126 printThreadState(t);
127 synchronized(cdl2) {
128 cdl2.notifyAll();
129 }
130
131 // Blocked on monitor.
132 synchronized(cdl3_2) {
133 cdl3_1.countDown();
134 cdl3_2.await();
135 Thread.yield();
136 Thread.sleep(100);
137 printThreadState(t);
138 }
139
140 // Sleeping.
141 cdl4.await();
142 Thread.yield();
143 Thread.sleep(100);
144 printThreadState(t);
145
146 // Running.
147 cdl5.await();
148 Thread.yield();
149 Thread.sleep(100);
150 printThreadState(t);
151 h.flag = true;
152
153 // Dying.
154 t.join();
155 Thread.yield();
156 Thread.sleep(100);
157
158 printThreadState(t);
159 }
160
Andreas Gampe85807442017-01-13 14:40:58 -0800161 private static void doAllThreadsTests() {
162 Thread[] threads = getAllThreads();
163 Arrays.sort(threads, THREAD_COMP);
164 System.out.println(Arrays.toString(threads));
165 }
166
167 private final static Comparator<Thread> THREAD_COMP = new Comparator<Thread>() {
168 public int compare(Thread o1, Thread o2) {
169 return o1.getName().compareTo(o2.getName());
170 }
171 };
172
Andreas Gampe72c19832017-01-12 13:22:16 -0800173 private final static Map<Integer, String> STATE_NAMES = new HashMap<Integer, String>();
174 private final static List<Integer> STATE_KEYS = new ArrayList<Integer>();
175 static {
176 STATE_NAMES.put(0x1, "ALIVE");
177 STATE_NAMES.put(0x2, "TERMINATED");
178 STATE_NAMES.put(0x4, "RUNNABLE");
179 STATE_NAMES.put(0x400, "BLOCKED_ON_MONITOR_ENTER");
180 STATE_NAMES.put(0x80, "WAITING");
181 STATE_NAMES.put(0x10, "WAITING_INDEFINITELY");
182 STATE_NAMES.put(0x20, "WAITING_WITH_TIMEOUT");
183 STATE_NAMES.put(0x40, "SLEEPING");
184 STATE_NAMES.put(0x100, "IN_OBJECT_WAIT");
185 STATE_NAMES.put(0x200, "PARKED");
186 STATE_NAMES.put(0x100000, "SUSPENDED");
187 STATE_NAMES.put(0x200000, "INTERRUPTED");
188 STATE_NAMES.put(0x400000, "IN_NATIVE");
189 STATE_KEYS.addAll(STATE_NAMES.keySet());
190 Collections.sort(STATE_KEYS);
191 }
192
193 private static void printThreadState(Thread t) {
194 int state = getThreadState(t);
195
196 StringBuilder sb = new StringBuilder();
197
198 for (Integer i : STATE_KEYS) {
199 if ((state & i) != 0) {
200 if (sb.length()>0) {
201 sb.append('|');
202 }
203 sb.append(STATE_NAMES.get(i));
204 }
205 }
206
207 if (sb.length() == 0) {
208 sb.append("NEW");
209 }
210
211 System.out.println(Integer.toHexString(state) + " = " + sb.toString());
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800212 }
213
214 private static void printThreadInfo(Thread t) {
215 Object[] threadInfo = getThreadInfo(t);
216 if (threadInfo == null || threadInfo.length != 5) {
217 System.out.println(Arrays.toString(threadInfo));
218 throw new RuntimeException("threadInfo length wrong");
219 }
220
221 System.out.println(threadInfo[0]); // Name
222 System.out.println(threadInfo[1]); // Priority
223 System.out.println(threadInfo[2]); // Daemon
224 System.out.println(threadInfo[3]); // Threadgroup
225 System.out.println(threadInfo[4] == null ? "null" : threadInfo[4].getClass()); // Context CL.
226 }
227
228 private static native Thread getCurrentThread();
229 private static native Object[] getThreadInfo(Thread t);
Andreas Gampe72c19832017-01-12 13:22:16 -0800230 private static native int getThreadState(Thread t);
Andreas Gampe85807442017-01-13 14:40:58 -0800231 private static native Thread[] getAllThreads();
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800232}