7197919: java/lang/management/ThreadMXBean/ThreadBlockedCount.java has concurency issues

Reviewed-by: sla, mchung
This commit is contained in:
Jaroslav Bachorik 2013-10-21 10:40:03 +02:00
parent b79a0fda0f
commit d491a49652

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2013, 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
@ -32,7 +32,6 @@
*/
import java.lang.management.*;
import java.util.concurrent.locks.LockSupport;
public class ThreadBlockedCount {
final static long EXPECTED_BLOCKED_COUNT = 3;
@ -101,6 +100,7 @@ public class ThreadBlockedCount {
System.err.println("Unexpected exception.");
e.printStackTrace(System.err);
testFailed = true;
break;
}
}
@ -127,6 +127,7 @@ public class ThreadBlockedCount {
System.err.println("Unexpected exception.");
e.printStackTrace(System.err);
testFailed = true;
break;
}
}
@ -153,6 +154,7 @@ public class ThreadBlockedCount {
System.err.println("Unexpected exception.");
e.printStackTrace(System.err);
testFailed = true;
break;
}
}
@ -166,11 +168,21 @@ public class ThreadBlockedCount {
}
}
// Check the mbean now
ThreadInfo ti = mbean.getThreadInfo(Thread.currentThread().
getId());
long count = ti.getBlockedCount();
// wait for the thread stats to be updated for 10 seconds
for (int i = 0; i < 100; i++) {
if (getBlockedCount() == EXPECTED_BLOCKED_COUNT) {
return;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.err.println("Unexpected exception.");
e.printStackTrace(System.err);
testFailed = true;
return;
}
}
long count = getBlockedCount();
if (count != EXPECTED_BLOCKED_COUNT) {
System.err.println("TEST FAILED: Blocked thread has " + count +
" blocked counts. Expected " +
@ -195,6 +207,7 @@ public class ThreadBlockedCount {
System.err.println("Unexpected exception.");
e.printStackTrace(System.err);
testFailed = true;
break;
}
ThreadInfo info = mbean.getThreadInfo(blocked.getId());
threadBlocked = (info.getThreadState() == Thread.State.BLOCKED);
@ -233,4 +246,13 @@ public class ThreadBlockedCount {
} // run()
} // BlockedThread
private static long getBlockedCount() {
long count;
// Check the mbean now
ThreadInfo ti = mbean.getThreadInfo(Thread.currentThread().
getId());
count = ti.getBlockedCount();
return count;
}
}