/*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This source code is provided to illustrate the usage of a given feature
* or technique and has been deliberately simplified. Additional steps
* required for a production-quality application, such as security checks,
* input validation and proper error handling, might not be present in
* this sample code.
*/
/*
*/
import javax.management.*;
/**
* Example of using the java.lang.management API to dump stack trace
* and to perform deadlock detection.
*
* @author Mandy Chung
*/
public class ThreadMonitor {
// default - JDK 6+ VM
private boolean canDumpLocks = true;
/**
* Constructs a ThreadMonitor object to get thread information
* in a remote JVM.
*/
ThreadMXBean.class);
try {
} catch (MalformedObjectNameException e) {
// should not reach here
throw ie;
}
}
/**
* Constructs a ThreadMonitor object to get thread information
* in the local JVM.
*/
public ThreadMonitor() {
this.tmbean = getThreadMXBean();
}
/**
* Prints the thread dump information to System.out.
*/
public void threadDump() {
if (canDumpLocks) {
if (tmbean.isObjectMonitorUsageSupported() &&
// Print lock info if both object monitor usage
// and synchronizer usage are supported.
// This sample code can be modified to handle if
// either monitor usage or synchronizer usage is supported.
}
} else {
}
}
private void dumpThreadInfo() {
}
}
/**
* Prints the thread dump information with locks info to System.out.
*/
private void dumpThreadInfoWithLocks() {
}
}
// print thread information
// print stack trace with locks
if (mi.getLockedStackDepth() == i) {
}
}
}
}
}
if (ti.isSuspended()) {
}
if (ti.isInNative()) {
}
}
}
}
}
}
}
/**
* Checks if any threads are deadlocked. If any, print
* the thread dump information.
*/
public boolean findDeadlock() {
long[] tids;
return false;
}
}
} else {
return false;
}
// print thread information
}
}
return true;
}
try {
// look for findDeadlockedThreads operations;
boolean found = false;
found = true;
break;
}
}
if (!found) {
// if findDeadlockedThreads operation doesn't exist,
// the target VM is running on JDK 5 and details about
// synchronizers and locks cannot be dumped.
findDeadlocksMethodName = "findMonitorDeadlockedThreads";
canDumpLocks = false;
}
} catch (IntrospectionException e) {
throw ie;
} catch (InstanceNotFoundException e) {
throw ie;
} catch (ReflectionException e) {
throw ie;
}
}
}