1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
/*
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4530538
* @summary Basic unit test of ThreadInfo.getStackTrace() and
* ThreadInfo.getThreadState()
* @author Mandy Chung
*
* @run build Utils
* @run main ThreadStackTrace
*/
import java.lang.management.*;
import java.util.concurrent.Phaser;
public class ThreadStackTrace {
private static final ThreadMXBean mbean
= ManagementFactory.getThreadMXBean();
private static boolean notified = false;
private static final Object lockA = new Object();
private static final Object lockB = new Object();
private static volatile boolean testFailed = false;
private static final String[] blockedStack = {"run", "test", "A", "B", "C", "D"};
private static final int bsDepth = 6;
private static final int methodB = 4;
private static final String[] examinerStack = {"run", "examine1", "examine2"};
private static final int esDepth = 3;
private static final int methodExamine1= 2;
private static void checkNullThreadInfo(Thread t) throws Exception {
ThreadInfo ti = mbean.getThreadInfo(t.getId());
if (ti != null) {
ThreadInfo info =
mbean.getThreadInfo(t.getId(), Integer.MAX_VALUE);
System.out.println(INDENT + "TEST FAILED:");
if (info != null) {
printStack(t, info.getStackTrace());
System.out.println(INDENT + "Thread state: " + info.getThreadState());
}
throw new RuntimeException("TEST FAILED: " +
"getThreadInfo() is expected to return null for " + t);
}
}
private static boolean trace = false;
public static void main(String args[]) throws Exception {
if (args.length > 0 && args[0].equals("trace")) {
trace = true;
}
final Phaser p = new Phaser(2);
Examiner examiner = new Examiner("Examiner", p);
BlockedThread blocked = new BlockedThread("BlockedThread", p);
examiner.setThread(blocked);
checkNullThreadInfo(examiner);
checkNullThreadInfo(blocked);
// Start the threads and check them in Blocked and Waiting states
examiner.start();
// #1 - block until examiner begins doing its real work
p.arriveAndAwaitAdvance();
System.out.println("Checking stack trace for the examiner thread " +
"is waiting to begin.");
// The Examiner should be waiting to be notified by the BlockedThread
Utils.checkThreadState(examiner, Thread.State.WAITING);
// Check that the stack is returned correctly for a new thread
checkStack(examiner, examinerStack, esDepth);
System.out.println("Now starting the blocked thread");
blocked.start();
try {
examiner.join();
blocked.join();
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("Unexpected exception.");
testFailed = true;
}
// Check that the stack is returned correctly for a terminated thread
checkNullThreadInfo(examiner);
checkNullThreadInfo(blocked);
if (testFailed)
throw new RuntimeException("TEST FAILED.");
System.out.println("Test passed.");
}
private static String INDENT = " ";
private static void printStack(Thread t, StackTraceElement[] stack) {
System.out.println(INDENT + t +
" stack: (length = " + stack.length + ")");
if (t != null) {
for (int j = 0; j < stack.length; j++) {
System.out.println(INDENT + stack[j]);
}
System.out.println();
}
}
private static void checkStack(Thread t, String[] expectedStack,
int depth) throws Exception {
ThreadInfo ti = mbean.getThreadInfo(t.getId(), Integer.MAX_VALUE);
StackTraceElement[] stack = ti.getStackTrace();
if (trace) {
printStack(t, stack);
}
int frame = stack.length - 1;
for (int i = 0; i < depth; i++) {
if (! stack[frame].getMethodName().equals(expectedStack[i])) {
throw new RuntimeException("TEST FAILED: " +
"Expected " + expectedStack[i] + " in frame " + frame +
" but got " + stack[frame].getMethodName());
}
frame--;
}
}
static class BlockedThread extends Thread {
private final Phaser phaser;
BlockedThread(String name, Phaser phaser) {
super(name);
this.phaser = phaser;
}
private void test() {
A();
}
private void A() {
B();
}
private void B() {
C();
// #4 - notify the examiner about to block on lockB
phaser.arriveAndAwaitAdvance();
synchronized (lockB) {};
}
private void C() {
D();
}
private void D() {
// #2 - Notify that examiner about to enter lockA
phaser.arriveAndAwaitAdvance();
synchronized (lockA) {
notified = false;
while (!notified) {
try {
// #3 - notify the examiner about to release lockA
phaser.arriveAndAwaitAdvance();
// Wait and let examiner thread check the mbean
lockA.wait();
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("Unexpected exception.");
testFailed = true;
}
}
System.out.println("BlockedThread notified");
}
}
@Override
public void run() {
test();
} // run()
} // BlockedThread
static class Examiner extends Thread {
private static BlockedThread blockedThread;
private final Phaser phaser;
Examiner(String name, Phaser phaser) {
super(name);
this.phaser = phaser;
}
public void setThread(BlockedThread thread) {
blockedThread = thread;
}
private Thread itself;
private void examine1() {
synchronized (lockB) {
examine2();
try {
System.out.println("Checking examiner's its own stack trace");
Utils.checkThreadState(itself, Thread.State.RUNNABLE);
checkStack(itself, examinerStack, methodExamine1);
// #4 - wait until blockedThread is blocked on lockB
phaser.arriveAndAwaitAdvance();
Utils.waitForThreadState(blockedThread, State.BLOCKED);
System.out.println("Checking stack trace for " +
"BlockedThread - should be blocked on lockB.");
Utils.checkThreadState(blockedThread, Thread.State.BLOCKED);
checkStack(blockedThread, blockedStack, methodB);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Unexpected exception.");
testFailed = true;
}
}
}
private void examine2() {
synchronized (lockA) {
// #1 - examiner ready to do the real work
phaser.arriveAndAwaitAdvance();
try {
// #2 - Wait until BlockedThread is about to block on lockA
phaser.arriveAndAwaitAdvance();
Utils.waitForThreadState(blockedThread, State.BLOCKED);
System.out.println("Checking examiner's its own stack trace");
Utils.checkThreadState(itself, Thread.State.RUNNABLE);
checkStack(itself, examinerStack, esDepth);
System.out.println("Checking stack trace for " +
"BlockedThread - should be blocked on lockA.");
Utils.checkThreadState(blockedThread, Thread.State.BLOCKED);
checkStack(blockedThread, blockedStack, bsDepth);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Unexpected exception.");
testFailed = true;
}
}
// #3 - release lockA and let BlockedThread to get the lock
// and wait on lockA
phaser.arriveAndAwaitAdvance();
Utils.waitForThreadState(blockedThread, State.WAITING);
synchronized (lockA) {
try {
System.out.println("Checking stack trace for " +
"BlockedThread - should be waiting on lockA.");
Utils.checkThreadState(blockedThread, Thread.State.WAITING);
checkStack(blockedThread, blockedStack, bsDepth);
// Let the blocked thread go
notified = true;
lockA.notify();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Unexpected exception.");
testFailed = true;
}
}
// give some time for BlockedThread to proceed
Utils.goSleep(50);
} // examine2()
@Override
public void run() {
itself = Thread.currentThread();
examine1();
} // run()
} // Examiner
}
|