JavaRuntime jRuntime = (JavaRuntime)runtimes.next();
Iterator monitors = jRuntime.getMonitors().iterator();
out.println("\nSystem locks...");
while (monitors.hasNext()){
JavaMonitor jMonitor = (JavaMonitor)monitors.next();
JavaObject jObject = jMonitor.getObject();
try {
String monitorName = jMonitor.getName().trim();
if (monitorName.equalsIgnoreCase("")) {
monitorName = "<un-named monitor>";
}
out.println("id: 0x" + jMonitor.getID() + " name: " + jMonitor.getName());
JavaThread owner = jMonitor.getOwner();
if (null != owner) {
try {
out.println("\towner thread id: " + owner.getImageThread().getID()
+ " name: " + owner.getName());
} catch (DataUnavailable e) {
out.println("\towner thread id: " + Exceptions.getDataUnavailableString());
}
}
// List any threads waiting on enter or notify for this monitor
Iterator itEnterWaiter = jMonitor.getEnterWaiters().iterator();
while (itEnterWaiter.hasNext()) {
JavaThread jThread = (JavaThread)itEnterWaiter.next();
try {
out.println("\twaiting thread id: " + jThread.getImageThread().getID()
+ " name: " + jThread.getName());
} catch (DataUnavailable dae) {
out.println("\twaiting thread id: <unknown>");
}
}
Iterator itNotifyWaiter = jMonitor.getNotifyWaiters().iterator();
while (itNotifyWaiter.hasNext()) {
JavaThread jThread = (JavaThread)itNotifyWaiter.next();
try {
out.println("\twaiting thread id: " + jThread.getImageThread().getID()
+ " name: " + jThread.getName());
} catch (DataUnavailable dae) {
out.println("\twaiting thread id: <unknown>");
}
}
} catch(CorruptDataException cde) {
out.println("\nwarning, corrupt data encountered during scan for system locks...");
}
if (null != jObject) {
// Remember object monitors (flat or inflated) for later
vMonitorsWithLockedObjects.add(jMonitor);
}
}
}
out.println("\nLocked objects...");
if (0 == vMonitorsWithLockedObjects.size()){
out.println("\t...None.");
return;
}
Iterator lockedObjects = vMonitorsWithLockedObjects.iterator();
while(lockedObjects.hasNext()){
JavaMonitor jMonitor = (JavaMonitor)lockedObjects.next();
JavaObject jObject = jMonitor.getObject();
try{
JavaThread owner = jMonitor.getOwner();
String className = "<unknown class>";
JavaClass jClass = jObject.getJavaClass();
if (null != jClass) {
className = jClass.getName();
}