Example usage for java.lang.management ManagementFactory getThreadMXBean

List of usage examples for java.lang.management ManagementFactory getThreadMXBean

Introduction

In this page you can find the example usage for java.lang.management ManagementFactory getThreadMXBean.

Prototype

public static ThreadMXBean getThreadMXBean() 

Source Link

Document

Returns the managed bean for the thread system of the Java virtual machine.

Usage

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

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

From source file:io.werval.modules.metrics.Tools.java

public Outcome threadDump() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new ThreadDump(ManagementFactory.getThreadMXBean()).dump(baos);
    return outcomes().ok(baos.toByteArray()).asTextPlain().build();
}

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//from w ww.  j ava 2s  .c om
 *         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:morphy.service.ThreadService.java

/**
 * Dumps stack traces of all threads to threaddump.txt.
 *///from w w w  .  j av a 2 s. c o m
public void threadDump() {
    LOG.error("All threads are in use. Logging the thread stack trace to threaddump.txt and exiting.");
    final ThreadMXBean threads = ManagementFactory.getThreadMXBean();
    long[] threadIds = threads.getAllThreadIds();
    PrintWriter printWriter = null;
    try {
        printWriter = new PrintWriter(new FileWriter(THREAD_DUMP_FILE_PATH, false));
        printWriter.println("Morphy ThreadService initiated dump " + new Date());
        for (long threadId : threadIds) {
            ThreadInfo threadInfo = threads.getThreadInfo(threadId, 10);
            printWriter.println("Thread " + threadInfo.getThreadName() + " Block time:"
                    + threadInfo.getBlockedTime() + " Block count:" + threadInfo.getBlockedCount()
                    + " Lock name:" + threadInfo.getLockName() + " Waited Count:" + threadInfo.getWaitedCount()
                    + " Waited Time:" + threadInfo.getWaitedTime() + " Is Suspended:"
                    + threadInfo.isSuspended());
            StackTraceElement[] stackTrace = threadInfo.getStackTrace();
            for (StackTraceElement element : stackTrace) {
                printWriter.println(element);
            }

        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        if (printWriter != null) {
            try {
                printWriter.flush();
                printWriter.close();
            } catch (Exception e2) {
            }
        }
    }
}

From source file:com.francetelecom.clara.cloud.presentation.ExceptionViewFactory.java

public ExceptionView newView(Exception e) {
    Throwable rootCause = (ExceptionUtils.getRootCause(e) != null ? ExceptionUtils.getRootCause(e) : e);
    if (rootCause instanceof AuthorizationException) {
        return webPageFactory.getAuthorizationExceptionPage();
    }/*from www.j a v a 2  s  .c om*/
    if (rootCause instanceof InvalidApplicationException) {
        return webPageFactory.getInvalidApplicationExceptionPage();
    }
    if (rootCause instanceof InvalidReleaseException) {
        return webPageFactory.getInvalidReleaseExceptionPage();
    }
    if (rootCause instanceof ObjectNotFoundException) {
        return webPageFactory.getObjectNotFoundExceptionPage();
    }
    if (rootCause instanceof WicketRuntimeException && rootCause.getMessage() != null
            && rootCause.getMessage().contains("Pagemap null is still locked")) {
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        logger.debug("dumping threads for page map issue");
        logger.debug(threadMXBean.dumpAllThreads(true, true).toString());
        return webPageFactory.getUnknownExceptionPage();
    }

    return webPageFactory.getUnknownExceptionPage();
}

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

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

From source file:io.pcp.parfait.benchmark.CPUThreadTest.java

private void runBenchmark(boolean cpuTracingEnabled, CPUThreadTestRunner.CpuLookupMethod cpuLookupMethod) {
    ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
    List<CPUThreadTestRunner> executions = newArrayList();

    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    threadBean.setThreadCpuTimeEnabled(cpuTracingEnabled);

    threadBean.setThreadContentionMonitoringEnabled(true);

    long begin = currentTimeMillis();

    for (int i = 0; i < numThreads; i++) {
        CPUThreadTestRunner cpuThreadTestRunner = new CPUThreadTestRunner(iterations, cpuLookupMethod);
        executorService.execute(cpuThreadTestRunner);
        executions.add(cpuThreadTestRunner);
    }//  ww w  . ja  v a 2s  . com

    awaitExecutionCompletion(executorService);

    long end = currentTimeMillis();
    long timeTakenms = end - begin;

    report(executions, timeTakenms, iterations, cpuTracingEnabled, cpuLookupMethod);
}

From source file:org.apache.tajo.master.querymaster.QueryMasterRunner.java

public static void printThreadInfo(PrintWriter stream, String title) {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    final int STACK_DEPTH = 60;
    boolean contention = threadBean.isThreadContentionMonitoringEnabled();
    long[] threadIds = threadBean.getAllThreadIds();
    stream.println("Process Thread Dump: " + title);
    stream.println(threadIds.length + " active threads");
    for (long tid : threadIds) {
        ThreadInfo info = threadBean.getThreadInfo(tid, STACK_DEPTH);
        if (info == null) {
            stream.println("  Inactive");
            continue;
        }//w w w . j  a  v  a  2s  .  c om
        stream.println("Thread " + getTaskName(info.getThreadId(), info.getThreadName()) + ":");
        Thread.State state = info.getThreadState();
        stream.println("  State: " + state);
        stream.println("  Blocked count: " + info.getBlockedCount());
        stream.println("  Waited count: " + info.getWaitedCount());
        if (contention) {
            stream.println("  Blocked time: " + info.getBlockedTime());
            stream.println("  Waited time: " + info.getWaitedTime());
        }
        if (state == Thread.State.WAITING) {
            stream.println("  Waiting on " + info.getLockName());
        } else if (state == Thread.State.BLOCKED) {
            stream.println("  Blocked on " + info.getLockName());
            stream.println("  Blocked by " + getTaskName(info.getLockOwnerId(), info.getLockOwnerName()));
        }
        stream.println("  Stack:");
        for (StackTraceElement frame : info.getStackTrace()) {
            stream.println("    " + frame.toString());
        }
    }
    stream.flush();
}

From source file:com.l2jfree.gameserver.threadmanager.DeadlockDetector.java

private long[] findDeadlockedThreadIds() {
    if (ManagementFactory.getThreadMXBean().isSynchronizerUsageSupported())
        return ManagementFactory.getThreadMXBean().findDeadlockedThreads();
    else/*from   w w w .  j  ava2 s.  c o m*/
        return ManagementFactory.getThreadMXBean().findMonitorDeadlockedThreads();
}

From source file:pt.minha.calibration.AbstractBenchmark.java

protected void start() {
    mxbean = ManagementFactory.getThreadMXBean();
    mxbean.setThreadCpuTimeEnabled(true);
    cputime = mxbean.getCurrentThreadCpuTime();
}