Example usage for java.lang.management ThreadMXBean findDeadlockedThreads

List of usage examples for java.lang.management ThreadMXBean findDeadlockedThreads

Introduction

In this page you can find the example usage for java.lang.management ThreadMXBean findDeadlockedThreads.

Prototype

public long[] findDeadlockedThreads();

Source Link

Document

Finds cycles of threads that are in deadlock waiting to acquire object monitors or ownable synchronizers.

Usage

From source file:Main.java

/**
 * //from   w ww  .j  a  va 2 s  .c o  m
 */
public static long[] getDeadlocks() {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    return bean.findDeadlockedThreads();
}

From source file:Main.java

public static String dumpThreads() {
    StringBuilder sb = new StringBuilder();
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    long[] ids = bean.getAllThreadIds();
    _printThreads(bean, ids, sb);/*from ww  w . j a va  2 s.  c o m*/
    long[] deadlocks = bean.findDeadlockedThreads();
    if (deadlocks != null && deadlocks.length > 0) {
        sb.append("deadlocked threads:\n");
        _printThreads(bean, deadlocks, sb);
    }

    deadlocks = bean.findMonitorDeadlockedThreads();
    if (deadlocks != null && deadlocks.length > 0) {
        sb.append("monitor deadlocked threads:\n");
        _printThreads(bean, deadlocks, sb);
    }
    return sb.toString();
}

From source file:Main.java

private static void printDeadlockInfo(StringBuilder builder, ThreadMXBean bean) {
    long[] ids = bean.getAllThreadIds();
    long[] dds = bean.findDeadlockedThreads();
    if (dds != null) {
        ThreadInfo[] info = bean.getThreadInfo(ids, true, true);
        builder.append("The following threads are deadlocked:");
        for (ThreadInfo ti : info) {
            builder.append("\t").append(ti);
        }//  w w  w  .j ava 2s  .  c o  m
    }
}

From source file:net.bull.javamelody.internal.model.JavaInformations.java

private static long[] getDeadlockedThreads(ThreadMXBean threadBean) {
    final long[] deadlockedThreads;
    if (threadBean.isSynchronizerUsageSupported()) {
        deadlockedThreads = threadBean.findDeadlockedThreads();
    } else {//from   www  .ja  v  a2  s. c o  m
        deadlockedThreads = threadBean.findMonitorDeadlockedThreads();
    }
    if (deadlockedThreads != null) {
        Arrays.sort(deadlockedThreads);
    }
    return deadlockedThreads;
}

From source file:com.amazonaws.client.metrics.support.JmxInfoProviderSupport.java

@Override
public long[] findDeadlockedThreads() {
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    return threadMXBean.findDeadlockedThreads();
}

From source file:com.workplacesystems.utilsj.ThreadDumperJdk16.java

@Override
void outputDeadlockedThreads(ThreadMXBean bean, StringBuffer buffer) {
    long[] deadlocked = bean.findDeadlockedThreads();

    if (deadlocked != null && deadlocked.length > 0) {
        buffer.append("Deadlock detected. Threads: ");
        for (int i = 0; i < deadlocked.length; i++) {
            if (i != 0)
                buffer.append(", ");
            buffer.append(deadlocked[i]);
        }/*from w  w  w. j a v  a 2  s .co  m*/
        buffer.append("\n");
    }
}

From source file:DeadlockDetector.java

@Override
public void run() {
    boolean noDeadLocks = true;

    while (noDeadLocks) {
        try {/*from  w  w w  .j a  v a  2 s  . c om*/
            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("\nTHREAD LOCK INFO: \n");
                for (ThreadInfo threadInfo : infos) {
                    printThreadInfo(threadInfo);
                    LockInfo[] lockInfos = threadInfo.getLockedSynchronizers();
                    MonitorInfo[] monitorInfos = threadInfo.getLockedMonitors();

                    printLockInfo(lockInfos);
                    printMonitorInfo(threadInfo, monitorInfos);
                }

                sb.append("\nTHREAD DUMPS: \n");
                for (ThreadInfo ti : bean.dumpAllThreads(true, true)) {
                    printThreadInfo(ti);
                }
                System.out.println(sb.toString());
            }
            Thread.sleep(checkInterval);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

From source file:com.thoughtworks.go.server.service.support.ThreadInformationProvider.java

private Map<String, Object> getDeadLockThreadInformation(ThreadMXBean threadMXBean) {
    LinkedHashMap<String, Object> json = new LinkedHashMap<>();
    long[] deadlockedThreads = threadMXBean.findDeadlockedThreads();

    if (deadlockedThreads != null && deadlockedThreads.length > 0) {
        json.put("Count", deadlockedThreads.length);
        for (long deadlockedThread : deadlockedThreads) {
            LinkedHashMap<String, Object> threadsInfo = new LinkedHashMap<>();
            LinkedHashMap<String, Object> lockedMonitorsInfo = new LinkedHashMap<>();
            LinkedHashMap<String, Object> stackTrackInfo = new LinkedHashMap<>();
            ThreadInfo threadInfo = threadMXBean.getThreadInfo(deadlockedThread);
            LockInfo lockInfo = threadInfo.getLockInfo();
            if (lockInfo != null) {
                threadsInfo.put(threadInfo.getThreadName(), lockInfo);
            } else {
                threadsInfo.put(threadInfo.getThreadName(), "This thread is not waiting for any locks");
            }//from w w w  . j a  va 2 s .  c  om
            MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
            for (MonitorInfo lockedMonitor : lockedMonitors) {
                lockedMonitorsInfo.put("Monitor for class " + lockedMonitor.getClassName(),
                        "taken at stack frame " + lockedMonitor.getLockedStackFrame());
            }
            stackTrackInfo.put(Long.toString(deadlockedThread), Arrays.toString(threadInfo.getStackTrace()));
            json.put("Thread Information", threadsInfo);
            json.put("Monitor Information Stack Frame where locks were taken", lockedMonitorsInfo);
            json.put("Stack Trace Of DeadLock Threads", stackTrackInfo);
        }
    }
    return json;
}

From source file:com.thoughtworks.go.server.service.support.ServerRuntimeInformationProvider.java

private void threadInfo(ThreadMXBean threadMXBean, InformationStringBuilder builder) {
    builder.addSection("Thread information");

    builder.append(String.format("Current: %s, Total: %s, Daemon: %s, Peak: %s\n",
            threadMXBean.getThreadCount(), threadMXBean.getTotalStartedThreadCount(),
            threadMXBean.getDaemonThreadCount(), threadMXBean.getPeakThreadCount()));
    long[] deadlockedThreads = threadMXBean.findDeadlockedThreads();
    if (deadlockedThreads != null && deadlockedThreads.length > 0) {
        builder.append(String.format("Found %s dead locked threads. Here is there information.\n",
                deadlockedThreads.length));
        for (long deadlockedThread : deadlockedThreads) {
            ThreadInfo threadInfo = threadMXBean.getThreadInfo(deadlockedThread);
            LockInfo lockInfo = threadInfo.getLockInfo();
            if (lockInfo != null) {
                builder.append(String.format("LockInfo: %s", lockInfo));
            } else {
                builder.append("This thread is not waiting for any locks\n");
            }//  w w  w .  j a  v  a 2s  .  c  o m
            builder.append(String.format("Monitor Info - Stack Frame where locks were taken.\n"));
            MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
            for (MonitorInfo lockedMonitor : lockedMonitors) {
                builder.append(String.format("Monitor for class '%s' taken at stack frame '%s'.",
                        lockedMonitor.getClassName(), lockedMonitor.getLockedStackFrame()));
            }
            builder.append("The stack trace of the deadlocked thread\n");
            builder.append(Arrays.toString(threadInfo.getStackTrace()));
        }
    }
    builder.addSubSection("All thread stacktraces");
    ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true);
    for (ThreadInfo threadInfo : threadInfos) {
        builder.append(String.format("%s, %s, %s\n", threadInfo.getThreadId(), threadInfo.getThreadName(),
                threadInfo.getThreadState()));
        MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
        builder.append("Locked Monitors:\n");
        for (MonitorInfo lockedMonitor : lockedMonitors) {
            builder.append(String.format("%s at %s", lockedMonitor, lockedMonitor.getLockedStackFrame()));
        }
        LockInfo[] lockedSynchronizers = threadInfo.getLockedSynchronizers();
        builder.append("Locked Synchronizers:\n");
        for (LockInfo lockedSynchronizer : lockedSynchronizers) {
            builder.append(lockedSynchronizer);
        }
        builder.append("Stacktrace:\n  ");
        builder.append(StringUtils.join(threadInfo.getStackTrace(), "\n  "));
        builder.append("\n\n");
    }
}

From source file:com.thoughtworks.go.server.service.support.ThreadInformationProvider.java

@Override
public void appendInformation(InformationStringBuilder builder) {
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    builder.addSection("Thread information");

    builder.append(String.format("Current: %s, Total: %s, Daemon: %s, Peak: %s\n",
            threadMXBean.getThreadCount(), threadMXBean.getTotalStartedThreadCount(),
            threadMXBean.getDaemonThreadCount(), threadMXBean.getPeakThreadCount()));
    long[] deadlockedThreads = threadMXBean.findDeadlockedThreads();
    if (deadlockedThreads != null && deadlockedThreads.length > 0) {
        builder.append(String.format("Found %s dead locked threads. Here is there information.\n",
                deadlockedThreads.length));
        for (long deadlockedThread : deadlockedThreads) {
            ThreadInfo threadInfo = threadMXBean.getThreadInfo(deadlockedThread);
            LockInfo lockInfo = threadInfo.getLockInfo();
            if (lockInfo != null) {
                builder.append(String.format("LockInfo: %s", lockInfo));
            } else {
                builder.append("This thread is not waiting for any locks\n");
            }/*from  ww w .j  av  a2 s  . c  o m*/
            builder.append(String.format("Monitor Info - Stack Frame where locks were taken.\n"));
            MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
            for (MonitorInfo lockedMonitor : lockedMonitors) {
                builder.append(String.format("Monitor for class '%s' taken at stack frame '%s'.",
                        lockedMonitor.getClassName(), lockedMonitor.getLockedStackFrame()));
            }
            builder.append("The stack trace of the deadlocked thread\n");
            builder.append(Arrays.toString(threadInfo.getStackTrace()));
        }
    }
    builder.addSubSection("All thread stacktraces");
    ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true);
    for (ThreadInfo threadInfo : threadInfos) {
        builder.append(String.format("%s, %s, %s\n", threadInfo.getThreadId(), threadInfo.getThreadName(),
                threadInfo.getThreadState()));
        MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
        builder.append("Locked Monitors:\n");
        for (MonitorInfo lockedMonitor : lockedMonitors) {
            builder.append(String.format("%s at %s", lockedMonitor, lockedMonitor.getLockedStackFrame()));
        }
        LockInfo[] lockedSynchronizers = threadInfo.getLockedSynchronizers();
        builder.append("Locked Synchronizers:\n");
        for (LockInfo lockedSynchronizer : lockedSynchronizers) {
            builder.append(lockedSynchronizer);
        }
        builder.append("Stacktrace:\n  ");
        builder.append(StringUtils.join(threadInfo.getStackTrace(), "\n  "));
        builder.append("\n\n");
    }
}