# HG changeset patch # User jbachorik # Date 1382344803 -7200 # Node ID e70ea8381dbde4cb23bd7941a934c677f2caef9b # Parent 9a71d04b2d0270a4e714be9921ad9413edc93f38 7197919: java/lang/management/ThreadMXBean/ThreadBlockedCount.java has concurency issues Reviewed-by: sla, mchung diff -r 9a71d04b2d02 -r e70ea8381dbd 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; + } }