Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/LockedMonitors.java
41152 views
/*1* Copyright (c) 2005, 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 5086470 635824726* @summary Basic Test for ThreadInfo.getLockedMonitors()27* - a stack frame acquires no monitor28* - a stack frame acquires one or more monitors29* - a stack frame blocks on Object.wait30* and the monitor waiting is not locked.31* LockingThread is the class that creates threads32* and do the checking.33*34* @author Mandy Chung35*36* @build Barrier37* @build LockingThread38* @build ThreadDump39* @run main/othervm LockedMonitors40*/4142import java.lang.management.*;43import java.util.*;4445public class LockedMonitors {46public static void main(String[] argv) throws Exception {47ThreadMXBean mbean = ManagementFactory.getThreadMXBean();48if (!mbean.isObjectMonitorUsageSupported()) {49System.out.println("Monitoring of object monitor usage is not supported");50return;51}5253// Start the thread and print the thread dump54LockingThread.startLockingThreads();55ThreadDump.threadDump();5657ThreadInfo[] tinfos;58long[] ids = LockingThread.getThreadIds();5960// Dump all threads with locked monitors61tinfos = mbean.dumpAllThreads(true, false);62LockingThread.checkLockedMonitors(tinfos);6364// Dump all threads with locked monitors and locked synchronizers65tinfos = mbean.dumpAllThreads(true, true);66LockingThread.checkLockedMonitors(tinfos);6768// Test getThreadInfo with locked monitors69tinfos = mbean.getThreadInfo(ids, true, false);70if (tinfos.length != ids.length) {71throw new RuntimeException("Number of ThreadInfo objects = " +72tinfos.length + " not matched. Expected: " + ids.length);73}74LockingThread.checkLockedMonitors(tinfos);7576// Test getThreadInfo with locked monitors and locked synchronizers77tinfos = mbean.getThreadInfo(ids, true, true);78if (tinfos.length != ids.length) {79throw new RuntimeException("Number of ThreadInfo objects = " +80tinfos.length + " not matched. Expected: " + ids.length);81}82LockingThread.checkLockedMonitors(tinfos);8384System.out.println("Test passed");85}86}878889