7197919: java/lang/management/ThreadMXBean/ThreadBlockedCount.java has concurency issues
authorjbachorik
Mon, 21 Oct 2013 10:40:03 +0200
changeset 21301 e70ea8381dbd
parent 21300 9a71d04b2d02
child 21302 c36804b5d972
7197919: java/lang/management/ThreadMXBean/ThreadBlockedCount.java has concurency issues Reviewed-by: sla, mchung
jdk/test/java/lang/management/ThreadMXBean/ThreadBlockedCount.java
--- a/jdk/test/java/lang/management/ThreadMXBean/ThreadBlockedCount.java	Sun Oct 20 18:07:40 2013 -0700
+++ b/jdk/test/java/lang/management/ThreadMXBean/ThreadBlockedCount.java	Mon Oct 21 10:40:03 2013 +0200
@@ -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 @@
                         System.err.println("Unexpected exception.");
                         e.printStackTrace(System.err);
                         testFailed = true;
+                        break;
                     }
                 }
 
@@ -127,6 +127,7 @@
                         System.err.println("Unexpected exception.");
                         e.printStackTrace(System.err);
                         testFailed = true;
+                        break;
                     }
                 }
 
@@ -153,6 +154,7 @@
                         System.err.println("Unexpected exception.");
                         e.printStackTrace(System.err);
                         testFailed = true;
+                        break;
                     }
                 }
 
@@ -166,15 +168,25 @@
                 }
             }
 
-            // 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 " +
-                                   EXPECTED_BLOCKED_COUNT);
+                                    " blocked counts. Expected " +
+                                    EXPECTED_BLOCKED_COUNT);
                 testFailed = true;
             }
         } // run()
@@ -195,6 +207,7 @@
                     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 @@
 
         } // run()
     } // BlockedThread
+
+    private static long getBlockedCount() {
+        long count;
+        // Check the mbean now
+        ThreadInfo ti = mbean.getThreadInfo(Thread.currentThread().
+                getId());
+        count = ti.getBlockedCount();
+        return count;
+    }
 }