Example usage for java.lang.management ThreadMXBean getThreadInfo

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

Introduction

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

Prototype

public ThreadInfo[] getThreadInfo(long[] ids);

Source Link

Document

Returns the thread info for each thread whose ID is in the input array ids with no stack trace.

Usage

From source file:Main.java

private static boolean threadExists(String namepart) {
    ThreadMXBean threadmx = ManagementFactory.getThreadMXBean();
    ThreadInfo[] infos = threadmx.getThreadInfo(threadmx.getAllThreadIds());
    for (ThreadInfo info : infos) {
        if (info != null) { // see javadoc getThreadInfo (thread can have disappeared between the two calls)
            if (info.getThreadName().contains(namepart)) {
                return true;
            }/*from  w ww .j a v a  2  s.  co m*/
        }
    }
    return false;
}

From source file:Main.java

/**
 * Get the thread info for the thread with the given ID.
 * A null is returned if no such thread info is found.
 *
 * @param   id   the thread ID to search for
 * @return      the thread info, or null if not found
 * @throws   IllegalArgumentException//ww w .  ja  v a2  s  .co  m
 *         if id <= 0
 */
public static ThreadInfo getThreadInfo(final long id) {
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();

    // Get thread info with lock info, when available.
    if (!thbean.isObjectMonitorUsageSupported() || !thbean.isSynchronizerUsageSupported())
        return thbean.getThreadInfo(id);

    final ThreadInfo[] infos = thbean.getThreadInfo(new long[] { id }, true, true);
    if (infos.length == 0)
        return null;
    return infos[0];
}

From source file:Main.java

/**
 * Get the thread info for the thread with the given ID. A null is returned
 * if no such thread info is found./*from  ww w. ja va 2  s.  c  o  m*/
 * 
 * @param id
 *        the thread ID to search for
 * @return the thread info, or null if not found
 * @throws IllegalArgumentException
 *         if id <= 0
 */
public static ThreadInfo getThreadInfo(final long id) {
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();

    // Get thread info with lock info, when available.
    if (!thbean.isObjectMonitorUsageSupported() || !thbean.isSynchronizerUsageSupported()) {
        return thbean.getThreadInfo(id);
    }

    final ThreadInfo[] infos = thbean.getThreadInfo(new long[] { id }, true, true);
    if (infos.length == 0)
        return null;
    return infos[0];
}

From source file:Main.java

public static void threadDumps() {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    long[] ids = bean.getAllThreadIds();
    StringBuilder builder = new StringBuilder();

    printTitle(builder);//  w  w  w .  ja  va2 s. c om

    printDeadlockInfo(builder, bean);

    for (long id : ids) {
        ThreadInfo info = bean.getThreadInfo(id);
        printThreadInfo(info, builder);
        printThreadSeparator(builder);
    }

    System.out.println(builder.toString());
}

From source file:Main.java

/**
 * Get a list of all thread info objects.  Since there is
 * always at least one thread running, there is always at
 * least one thread info object.  This method never returns
 * a null or empty array./*from   w  ww.  ja va2s .com*/
 *
 * @return      an array of thread infos
 */
public static ThreadInfo[] getAllThreadInfos() {
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    final long[] ids = thbean.getAllThreadIds();

    // Get thread info with lock info, when available.
    ThreadInfo[] infos;
    if (!thbean.isObjectMonitorUsageSupported() || !thbean.isSynchronizerUsageSupported())
        infos = thbean.getThreadInfo(ids);
    else
        infos = thbean.getThreadInfo(ids, true, true);

    // Clean nulls from array if threads have died.
    final ThreadInfo[] notNulls = new ThreadInfo[infos.length];
    int nNotNulls = 0;
    for (ThreadInfo info : infos)
        if (info != null)
            notNulls[nNotNulls++] = info;
    if (nNotNulls == infos.length)
        return infos; // Original had no nulls
    return java.util.Arrays.copyOf(notNulls, nNotNulls);
}

From source file:Main.java

/**
 * Get a list of all thread info objects. Since there is always at least one
 * thread running, there is always at least one thread info object. This
 * method never returns a null or empty array.
 * /*w ww .  j  av  a 2s . co  m*/
 * @return an array of thread infos
 */
public static ThreadInfo[] getAllThreadInfos() {
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    final long[] ids = thbean.getAllThreadIds();

    // Get thread info with lock info, when available.
    ThreadInfo[] infos;
    if (!thbean.isObjectMonitorUsageSupported() || !thbean.isSynchronizerUsageSupported()) {
        infos = thbean.getThreadInfo(ids);
    } else {
        infos = thbean.getThreadInfo(ids, true, true);
    }

    // Clean nulls from array if threads have died.
    final ThreadInfo[] notNulls = new ThreadInfo[infos.length];
    int nNotNulls = 0;
    for (ThreadInfo info : infos) {
        if (info != null) {
            notNulls[nNotNulls++] = info;
        }
    }
    if (nNotNulls == infos.length)
        return infos; // Original had no nulls
    return java.util.Arrays.copyOf(notNulls, nNotNulls);
}

From source file:models.monitor.MonitorProvider.java

public ThreadUsage getThreadUsage() {
    ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
    ThreadUsage threadUsage = new ThreadUsage();
    long[] threadIds = threadMxBean.getAllThreadIds();
    threadUsage.liveThreadCount = threadIds.length;

    for (long tId : threadIds) {
        ThreadInfo threadInfo = threadMxBean.getThreadInfo(tId);
        threadUsage.threadData.put(new Long(tId).toString(), new ThreadData(threadInfo.getThreadName(),
                threadInfo.getThreadState().name(), threadMxBean.getThreadCpuTime(tId)));

    }/*from  w  w w. jav  a 2s.c o m*/
    return threadUsage;
}

From source file:DeadlockDetector.java

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

    while (noDeadLocks) {
        try {//from   w w w.  j a  v a2s . com
            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");
            }/* w w w.  j a  v  a  2  s .  c  o  m*/
            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  ww .j  a va  2 s .  c  om*/
            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");
    }
}