Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/ThreadBlockedCount.java
41152 views
/*1* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 453053826* @summary Basic unit test of ThreadInfo.getBlockedCount()27* @author Alexei Guibadoulline and Mandy Chung28* @author Jaroslav Bachorik29*30* @run main ThreadBlockedCount31*/3233import java.lang.management.*;34import java.util.concurrent.Phaser;3536public class ThreadBlockedCount {37static final long EXPECTED_BLOCKED_COUNT = 3;38static final int DEPTH = 10;39private static final ThreadMXBean mbean40= ManagementFactory.getThreadMXBean();4142private static final Object a = new Object();43private static final Object b = new Object();44private static final Object c = new Object();4546private static final Object blockedObj1 = new Object();47private static final Object blockedObj2 = new Object();48private static final Object blockedObj3 = new Object();49private static volatile boolean testOk = true;50private static BlockingThread blocking;51private static BlockedThread blocked;5253public static void main(String args[]) throws Exception {54// real run55runTest();56if (!testOk) {57throw new RuntimeException("TEST FAILED.");58}59System.out.println("Test passed.");60}6162private static void runTest() throws Exception {63final Phaser p = new Phaser(2);6465blocking = new BlockingThread(p);66blocking.start();6768blocked = new BlockedThread(p);69blocked.start();7071try {72blocking.join();7374testOk = checkBlocked();75p.arriveAndAwaitAdvance(); // #57677} catch (InterruptedException e) {78System.err.println("Unexpected exception.");79e.printStackTrace(System.err);80throw e;81}82}838485static class BlockedThread extends Thread {86private final Phaser p;8788BlockedThread(Phaser p) {89super("BlockedThread");90this.p = p;91}9293public void run() {94int accumulator = 0;95p.arriveAndAwaitAdvance(); // #19697// Enter lock a without blocking98synchronized (a) {99p.arriveAndAwaitAdvance(); // #2100101// Block to enter blockedObj1102// blockedObj1 should be owned by BlockingThread103synchronized (blockedObj1) {104accumulator++; // filler105}106}107108// Enter lock a without blocking109synchronized (b) {110// wait until BlockingThread holds blockedObj2111p.arriveAndAwaitAdvance(); // #3112113// Block to enter blockedObj2114// blockedObj2 should be owned by BlockingThread115synchronized (blockedObj2) {116accumulator++; // filler117}118}119120// Enter lock a without blocking121synchronized (c) {122// wait until BlockingThread holds blockedObj3123p.arriveAndAwaitAdvance(); // #4124125// Block to enter blockedObj3126// blockedObj3 should be owned by BlockingThread127synchronized (blockedObj3) {128accumulator++; // filler129}130}131132// wait for the main thread to check the blocked count133System.out.println("Acquired " + accumulator + " monitors");134p.arriveAndAwaitAdvance(); // #5135// ... and we can leave now136} // run()137} // BlockedThread138139static class BlockingThread extends Thread {140private final Phaser p;141142BlockingThread(Phaser p) {143super("BlockingThread");144this.p = p;145}146147private void waitForBlocked() {148// wait for BlockedThread.149p.arriveAndAwaitAdvance();150151boolean threadBlocked = false;152while (!threadBlocked) {153// give a chance for BlockedThread to really block154try {155Thread.sleep(50);156} catch (InterruptedException e) {157System.err.println("Unexpected exception.");158e.printStackTrace(System.err);159testOk = false;160break;161}162ThreadInfo info = mbean.getThreadInfo(blocked.getId());163threadBlocked = (info.getThreadState() == Thread.State.BLOCKED);164}165}166167public void run() {168p.arriveAndAwaitAdvance(); // #1169170synchronized (blockedObj1) {171System.out.println("BlockingThread attempts to notify a");172waitForBlocked(); // #2173}174175// block until BlockedThread is ready176synchronized (blockedObj2) {177System.out.println("BlockingThread attempts to notify b");178waitForBlocked(); // #3179}180181// block until BlockedThread is ready182synchronized (blockedObj3) {183System.out.println("BlockingThread attempts to notify c");184waitForBlocked(); // #4185}186187} // run()188} // BlockingThread189190private static long getBlockedCount() {191long count;192// Check the mbean now193ThreadInfo ti = mbean.getThreadInfo(blocked.getId());194count = ti.getBlockedCount();195return count;196}197198private static boolean checkBlocked() {199// wait for the thread stats to be updated for 10 seconds200long count = -1;201for (int i = 0; i < 100; i++) {202count = getBlockedCount();203if (count >= EXPECTED_BLOCKED_COUNT) {204return true;205}206try {207Thread.sleep(100);208} catch (InterruptedException e) {209System.err.println("Unexpected exception.");210e.printStackTrace(System.err);211return false;212}213}214System.err.println("TEST FAILED: Blocked thread has " + count +215" blocked counts. Expected at least " +216EXPECTED_BLOCKED_COUNT);217return false;218}219}220221222