Source Code : Deadlock Detector
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Deadlock Detector
/*
* This file is part of aion-unique <aion-unique.org>.
*
* aion-unique is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* aion-unique is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with aion-unique. If not, see <http://www.gnu.org/licenses/>.
*/
//package com.aionemu.gameserver.utils;
import java.lang.management.LockInfo;
import java.lang.management.ManagementFactory;
import java.lang.management.MonitorInfo;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
/**
* @author ATracer
*/
public class DeadlockDetector implements Runnable
{
private int checkInterval = 0;
private static String INDENT = " ";
private StringBuilder sb = null;
public DeadlockDetector(int checkInterval)
{
this.checkInterval = checkInterval * 1000;
}
@Override
public void run()
{
boolean noDeadLocks = true;
while (noDeadLocks)
{
try
{
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
long[] threadIds = bean.findDeadlockedThreads();
if (threadIds != null)
{
System.out.println("Deadlock detected!");
sb = new StringBuilder();
noDeadLocks = false;
ThreadInfo[] infos = bean.getThreadInfo(threadIds);
sb.append("
THREAD LOCK INFO:
");
for (ThreadInfo threadInfo : infos)
{
printThreadInfo(threadInfo);
LockInfo[] lockInfos = threadInfo.getLockedSynchronizers();
MonitorInfo[] monitorInfos = threadInfo.getLockedMonitors();
printLockInfo(lockInfos);
printMonitorInfo(threadInfo, monitorInfos);
}
sb.append("
THREAD DUMPS:
");
for (ThreadInfo ti : bean.dumpAllThreads(true, true))
{
printThreadInfo(ti);
}
System.out.println(sb.toString());
}
Thread.sleep(checkInterval);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
private void printThreadInfo(ThreadInfo threadInfo)
{
printThread(threadInfo);
sb.append(INDENT + threadInfo.toString() + "
");
StackTraceElement[] stacktrace = threadInfo.getStackTrace();
MonitorInfo[] monitors = threadInfo.getLockedMonitors();
for (int i = 0; i < stacktrace.length; i++)
{
StackTraceElement ste = stacktrace[i];
sb.append(INDENT + "at " + ste.toString() + "
");
for (MonitorInfo mi : monitors)
{
if (mi.getLockedStackDepth() == i)
{
sb.append(INDENT + " - locked " + mi + "
");
}
}
}
}
private void printThread(ThreadInfo ti)
{
sb.append("
PrintThread
");
sb.append(""" + ti.getThreadName() + """ + " Id=" + ti.getThreadId() + " in " + ti.getThreadState() + "
");
if (ti.getLockName() != null)
{
sb.append(" on lock=" + ti.getLockName() + "
");
}
if (ti.isSuspended())
{
sb.append(" (suspended)" + "
");
}
if (ti.isInNative())
{
sb.append(" (running in native)" + "
");
}
if (ti.getLockOwnerName() != null)
{
sb.append(INDENT + " owned by " + ti.getLockOwnerName() + " Id=" + ti.getLockOwnerId() + "
");
}
}
private void printMonitorInfo(ThreadInfo threadInfo, MonitorInfo[] monitorInfos)
{
sb.append(INDENT + "Locked monitors: count = " + monitorInfos.length + "
");
for (MonitorInfo monitorInfo : monitorInfos)
{
sb.append(INDENT + " - " + monitorInfo + " locked at " + "
");
sb.append(INDENT + " " + monitorInfo.getLockedStackDepth() + " " + monitorInfo.getLockedStackFrame() + "
");
}
}
private void printLockInfo(LockInfo[] lockInfos)
{
sb.append(INDENT + "Locked synchronizers: count = " + lockInfos.length + "
");
for (LockInfo lockInfo : lockInfos)
{
sb.append(INDENT + " - " + lockInfo + "
");
}
}
}
Thank with us