1 /* 2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 5086470 6358247 27 * @summary Basic Test for ThreadInfo.getLockedMonitors() 28 * - a stack frame acquires no monitor 29 * - a stack frame acquires one or more monitors 30 * - a stack frame blocks on Object.wait 31 * and the monitor waiting is not locked. 32 * LockingThread is the class that creates threads 33 * and do the checking. 34 * 35 * @author Mandy Chung 36 * 37 * @modules java.management 38 * @build Barrier 39 * @build LockingThread 40 * @build ThreadDump 41 * @run main/othervm LockedMonitors 42 */ 43 44 import java.lang.management.*; 45 import java.util.*; 46 47 public class LockedMonitors { 48 public static void main(String[] argv) throws Exception { 49 ThreadMXBean mbean = ManagementFactory.getThreadMXBean(); 50 if (!mbean.isObjectMonitorUsageSupported()) { 51 System.out.println("Monitoring of object monitor usage is not supported"); 52 return; 53 } 54 55 // Start the thread and print the thread dump 56 LockingThread.startLockingThreads(); 57 ThreadDump.threadDump(); 58 59 ThreadInfo[] tinfos; 60 long[] ids = LockingThread.getThreadIds(); 61 62 // Dump all threads with locked monitors 63 tinfos = mbean.dumpAllThreads(true, false); 64 LockingThread.checkLockedMonitors(tinfos); 65 66 // Dump all threads with locked monitors and locked synchronizers 67 tinfos = mbean.dumpAllThreads(true, true); 68 LockingThread.checkLockedMonitors(tinfos); 69 70 // Test getThreadInfo with locked monitors 71 tinfos = mbean.getThreadInfo(ids, true, false); 72 if (tinfos.length != ids.length) { 73 throw new RuntimeException("Number of ThreadInfo objects = " + 74 tinfos.length + " not matched. Expected: " + ids.length); 75 } 76 LockingThread.checkLockedMonitors(tinfos); 77 78 // Test getThreadInfo with locked monitors and locked synchronizers 79 tinfos = mbean.getThreadInfo(ids, true, true); 80 if (tinfos.length != ids.length) { 81 throw new RuntimeException("Number of ThreadInfo objects = " + 82 tinfos.length + " not matched. Expected: " + ids.length); 83 } 84 LockingThread.checkLockedMonitors(tinfos); 85 86 System.out.println("Test passed"); 87 } 88 }