8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition

Reviewed-by: alanb, jfranck
This commit is contained in:
Peter Levart 2013-08-20 14:13:59 +02:00
parent 31584b6b19
commit 75a8f58cd1

View File

@ -28,6 +28,9 @@
*/ */
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -66,17 +69,6 @@ public class AnnotationTypeDeadlockTest {
} }
} }
static void dumpState(Task task) {
System.err.println(
"Task[" + task.getName() + "].state: " +
task.getState() + " ..."
);
for (StackTraceElement ste : task.getStackTrace()) {
System.err.println("\tat " + ste);
}
System.err.println();
}
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
CountDownLatch prepareLatch = new CountDownLatch(2); CountDownLatch prepareLatch = new CountDownLatch(2);
AtomicInteger goLatch = new AtomicInteger(1); AtomicInteger goLatch = new AtomicInteger(1);
@ -88,18 +80,22 @@ public class AnnotationTypeDeadlockTest {
prepareLatch.await(); prepareLatch.await();
// let them go // let them go
goLatch.set(0); goLatch.set(0);
// attempt to join them // obtain ThreadMXBean
taskA.join(5000L); ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
taskB.join(5000L); // wait for threads to finish or dead-lock
while (taskA.isAlive() || taskB.isAlive()) {
if (taskA.isAlive() || taskB.isAlive()) { // attempt to join threads
dumpState(taskA); taskA.join(500L);
dumpState(taskB); taskB.join(500L);
throw new IllegalStateException( // detect dead-lock
taskA.getState() == Thread.State.BLOCKED && long[] deadlockedIds = threadBean.findMonitorDeadlockedThreads();
taskB.getState() == Thread.State.BLOCKED if (deadlockedIds != null && deadlockedIds.length > 0) {
? "deadlock detected" StringBuilder sb = new StringBuilder("deadlock detected:\n\n");
: "unexpected condition"); for (ThreadInfo ti : threadBean.getThreadInfo(deadlockedIds, Integer.MAX_VALUE)) {
sb.append(ti);
}
throw new IllegalStateException(sb.toString());
}
} }
} }
} }