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: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  w  w w.  j  av  a 2 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");
    }
}

From source file:org.apache.tez.runtime.LogicalIOProcessorRuntimeTask.java

/**
 * Print all threads in JVM (only for debugging)
 *//*from  w ww . ja v  a 2  s .  c  o m*/
void printThreads() {
    //Print the status of all threads in JVM
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    long[] threadIds = threadMXBean.getAllThreadIds();
    for (Long id : threadIds) {
        ThreadInfo threadInfo = threadMXBean.getThreadInfo(id);
        // The thread could have been shutdown before we read info about it.
        if (threadInfo != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("ThreadId : " + id + ", name=" + threadInfo.getThreadName());
            }
        }
    }
}

From source file:org.wso2.carbon.integration.test.client.JMXAnalyzerClient.java

public static int getThreadCount(String host, String port) throws IOException {
    String username = "admin";
    String password = "admin";
    int threadCount = 0;
    String threadName = "JMSThreads";
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi");
    Map<String, String[]> env = new HashMap<String, String[]>();
    ThreadInfo threadIDInfo;/*from   ww w. j a  va 2  s .c  o m*/

    String[] credentials = { username, password };
    env.put(JMXConnector.CREDENTIALS, credentials);
    JMXConnector jmxConnector = JMXConnectorFactory.connect(url, env);
    MBeanServerConnection mbeanServerConnection = jmxConnector.getMBeanServerConnection();
    final ThreadMXBean remoteThread = ManagementFactory.newPlatformMXBeanProxy(mbeanServerConnection,
            ManagementFactory.THREAD_MXBEAN_NAME, ThreadMXBean.class);
    long[] allThreadIDsArray = remoteThread.getAllThreadIds();

    //get jms thread count
    for (long threadID : allThreadIDsArray) {
        threadIDInfo = remoteThread.getThreadInfo(threadID);
        if (threadIDInfo != null && threadIDInfo.getThreadName() != null
                && threadIDInfo.getThreadName().startsWith(threadName)) {
            threadCount++;
        }
    }
    //close the connection
    jmxConnector.close();
    return threadCount;
}

From source file:org.wyona.yanel.impl.resources.usecase.thread.UsecaseThread.java

/**
 * Get thread with the given id from repository.
 * @param repository Repository//  w w  w .ja v a2 s. c o  m
 * @param directoryPath Collection path which contains the thread key
 * @param threadID
 * @return thread or null if there is no such thread attached to the repository
 */
public static java.lang.management.ThreadInfo getThreadFromRepository(
        org.wyona.yarep.core.Repository repository, String directoryPath, String threadID)
        throws org.wyona.yarep.core.RepositoryException, java.io.IOException {
    //public static UsecaseThread getThreadFromRepository(org.wyona.yarep.core.Repository repository, String directoryPath, String threadID) throws org.wyona.yarep.core.RepositoryException, java.io.IOException {
    String threadKey = getThreadKey(threadID);
    String threadKeyPath = directoryPath + "/" + threadKey;
    if (repository.existsNode(threadKeyPath)) {
        Node node = repository.getNode(threadKeyPath);
        String javaThreadId = new java.io.BufferedReader(new java.io.InputStreamReader(node.getInputStream()))
                .readLine();

        final java.lang.management.ThreadMXBean thbean = java.lang.management.ManagementFactory
                .getThreadMXBean();
        long[] threadIDs = thbean.getAllThreadIds();
        for (int i = 0; i < threadIDs.length; i++) {
            log.debug("Thread ID: " + threadIDs[i]);
            if (javaThreadId.equals("" + threadIDs[i])) {
                log.debug("Thread with id '" + javaThreadId + "' is running.");
                return thbean.getThreadInfo(threadIDs[i]);
                /*
                                    final Thread[] threads = getAllThreads();
                                    for (Thread thread : threads) {
                if (thread.getId() == threadIDs[i]) {
                    return (UsecaseThread) thread;
                }
                                    }
                                    log.error("No thread with id '" + threadIDs[i] + "' exists!");
                                    return null;
                */
            }
        }
        log.info("No such thread running: " + threadID + ", " + javaThreadId);
        return null;
    } else {
        log.info("No such thread '" + threadID + "' within repository: " + threadKeyPath);
        return null;
    }
    //return (UsecaseThread)session.getAttribute(attrName); 
}