Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/ThreadMXBean/ThreadBlockedCount.java
38821 views
/*1* Copyright (c) 2003, 2013, 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* @run main ThreadBlockedCount30*/3132import java.lang.management.*;33import java.util.concurrent.Phaser;3435public class ThreadBlockedCount {36final static long EXPECTED_BLOCKED_COUNT = 3;37final static int DEPTH = 10;38private static final ThreadMXBean mbean39= ManagementFactory.getThreadMXBean();4041private static final Object a = new Object();42private static final Object b = new Object();43private static final Object c = new Object();4445private static final Object blockedObj1 = new Object();46private static final Object blockedObj2 = new Object();47private static final Object blockedObj3 = new Object();48private static volatile boolean testOk = true;49private static BlockingThread blocking;50private static BlockedThread blocked;5152public static void main(String args[]) throws Exception {53// real run54runTest();55if (!testOk) {56throw new RuntimeException("TEST FAILED.");57}58System.out.println("Test passed.");59}6061private static void runTest() throws Exception {62final Phaser p = new Phaser(2);6364blocking = new BlockingThread(p);65blocking.start();6667blocked = new BlockedThread(p);68blocked.start();6970try {71blocking.join();7273testOk = checkBlocked();74p.arriveAndAwaitAdvance(); // #57576} catch (InterruptedException e) {77System.err.println("Unexpected exception.");78e.printStackTrace(System.err);79throw e;80}81}828384static class BlockedThread extends Thread {85private final Phaser p;8687BlockedThread(Phaser p) {88super("BlockedThread");89this.p = p;90}9192public void run() {93int accumulator = 0;94p.arriveAndAwaitAdvance(); // #19596// Enter lock a without blocking97synchronized (a) {98p.arriveAndAwaitAdvance(); // #299100// Block to enter blockedObj1101// blockedObj1 should be owned by BlockingThread102synchronized (blockedObj1) {103accumulator++; // filler104}105}106107// Enter lock a without blocking108synchronized (b) {109// wait until BlockingThread holds blockedObj2110p.arriveAndAwaitAdvance(); // #3111112// Block to enter blockedObj2113// blockedObj2 should be owned by BlockingThread114synchronized (blockedObj2) {115accumulator++; // filler116}117}118119// Enter lock a without blocking120synchronized (c) {121// wait until BlockingThread holds blockedObj3122p.arriveAndAwaitAdvance(); // #4123124// Block to enter blockedObj3125// blockedObj3 should be owned by BlockingThread126synchronized (blockedObj3) {127accumulator++; // filler128}129}130131// wait for the main thread to check the blocked count132System.out.println("Acquired " + accumulator + " monitors");133p.arriveAndAwaitAdvance(); // #5134// ... and we can leave now135} // run()136} // BlockedThread137138static class BlockingThread extends Thread {139private final Phaser p;140141BlockingThread(Phaser p) {142super("BlockingThread");143this.p = p;144}145146private void waitForBlocked() {147// wait for BlockedThread.148p.arriveAndAwaitAdvance();149150boolean threadBlocked = false;151while (!threadBlocked) {152// give a chance for BlockedThread to really block153try {154Thread.sleep(50);155} catch (InterruptedException e) {156System.err.println("Unexpected exception.");157e.printStackTrace(System.err);158testOk = false;159break;160}161ThreadInfo info = mbean.getThreadInfo(blocked.getId());162threadBlocked = (info.getThreadState() == Thread.State.BLOCKED);163}164}165166public void run() {167p.arriveAndAwaitAdvance(); // #1168169synchronized (blockedObj1) {170System.out.println("BlockingThread attempts to notify a");171waitForBlocked(); // #2172}173174// block until BlockedThread is ready175synchronized (blockedObj2) {176System.out.println("BlockingThread attempts to notify b");177waitForBlocked(); // #3178}179180// block until BlockedThread is ready181synchronized (blockedObj3) {182System.out.println("BlockingThread attempts to notify c");183waitForBlocked(); // #4184}185186} // run()187} // BlockingThread188189private static long getBlockedCount() {190long count;191// Check the mbean now192ThreadInfo ti = mbean.getThreadInfo(blocked.getId());193count = ti.getBlockedCount();194return count;195}196197private static boolean checkBlocked() {198// wait for the thread stats to be updated for 10 seconds199long count = -1;200for (int i = 0; i < 100; i++) {201count = getBlockedCount();202if (count >= EXPECTED_BLOCKED_COUNT) {203return true;204}205try {206Thread.sleep(100);207} catch (InterruptedException e) {208System.err.println("Unexpected exception.");209e.printStackTrace(System.err);210return false;211}212}213System.err.println("TEST FAILED: Blocked thread has " + count +214" blocked counts. Expected at least " +215EXPECTED_BLOCKED_COUNT);216return false;217}218}219220221