1 /* 2 * Copyright (c) 2005, 2013, 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 * @build Barrier 38 * @build LockingThread 39 * @build ThreadDump 40 * @run main/othervm LockedMonitors 41 */ 42 43 import java.lang.management.*; 44 import java.util.*; 45 46 public class LockedMonitors { 47 public static void main(String[] argv) throws Exception { 48 ThreadMXBean mbean = ManagementFactory.getThreadMXBean(); 49 if (!mbean.isObjectMonitorUsageSupported()) { 50 System.out.println("Monitoring of object monitor usage is not supported"); 51 return; 52 } 53 54 // Start the thread and print the thread dump 55 LockingThread.startLockingThreads(); 56 ThreadDump.threadDump(); 57 58 ThreadInfo[] tinfos; 59 long[] ids = LockingThread.getThreadIds(); 60 61 // Dump all threads with locked monitors 62 tinfos = mbean.dumpAllThreads(true, false); 63 LockingThread.checkLockedMonitors(tinfos); 64 65 // Dump all threads with locked monitors and locked synchronizers 66 tinfos = mbean.dumpAllThreads(true, true); 67 LockingThread.checkLockedMonitors(tinfos); 68 69 // Test getThreadInfo with locked monitors 70 tinfos = mbean.getThreadInfo(ids, true, false); 71 if (tinfos.length != ids.length) { 72 throw new RuntimeException("Number of ThreadInfo objects = " + 73 tinfos.length + " not matched. Expected: " + ids.length); 74 } 75 LockingThread.checkLockedMonitors(tinfos); 76 77 // Test getThreadInfo with locked monitors and locked synchronizers 78 tinfos = mbean.getThreadInfo(ids, true, true); 79 if (tinfos.length != ids.length) { 80 throw new RuntimeException("Number of ThreadInfo objects = " + 81 tinfos.length + " not matched. Expected: " + ids.length); 82 } 83 LockingThread.checkLockedMonitors(tinfos); 84 85 System.out.println("Test passed"); 86 } 87 }