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:Main.java

/** Returns a list of all threads.
 *
 * @return a list of all threads//from  ww w .ja  va  2  s .  c  o  m
 */
public static List<Thread> getAllThreads() {
    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    int nAlloc = thbean.getThreadCount();
    @SuppressWarnings("UnusedAssignment")
    int n = 0;
    Thread[] threads;
    do {
        nAlloc *= 2;
        threads = new Thread[nAlloc];
        n = root.enumerate(threads, true);
    } while (n == nAlloc);
    final List<Thread> threadList = new ArrayList<>();
    for (final Thread thread : threads) {
        if (thread != null) {
            threadList.add(thread);
        }
    }
    return threadList;
}

From source file:Main.java

/**
 * Get a list of all threads.  Since there is always at
 * least one thread, this method never returns null or
 * an empty array./*w  ww .  ja  v a2 s. c o  m*/
 *
 * @return      an array of threads
 */
public static Thread[] getAllThreads() {
    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    int nAlloc = thbean.getThreadCount();
    int n = 0;
    Thread[] threads = null;
    do {
        nAlloc *= 2;
        threads = new Thread[nAlloc];
        n = root.enumerate(threads, true);
    } while (n == nAlloc);
    return threads.clone();
}

From source file:com.github.jinahya.codec.BossVsEngineerTestCpuTimeDecode.java

private static long decodeLikeABoss(final byte[] encoded) {
    final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    final long start = threadMXBean.getCurrentThreadCpuTime();
    new HexDecoder().decodeLikeABoss(encoded);
    return threadMXBean.getCurrentThreadCpuTime() - start;
}

From source file:com.github.jinahya.codec.BossVsEngineerTestCpuTimeEncode.java

private static long encodeLikeABoss(final byte[] decoded) {
    final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    final long start = threadMXBean.getCurrentThreadCpuTime();
    new HexEncoder().encodeLikeABoss(decoded);
    return threadMXBean.getCurrentThreadCpuTime() - start;
}

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  . j  a v a2  s  .c  om*/
 *
 * @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

public static Thread[] getAllThreads() {
    final ThreadGroup rootThreadGroup = getRootThreadGroup();
    final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

    int nAlloc = threadMXBean.getThreadCount();
    int n = 0;/*  ww  w. jav a  2 s  . c  o m*/
    Thread[] threads;

    do {
        nAlloc *= 2;
        threads = new Thread[nAlloc];
        n = rootThreadGroup.enumerate(threads, true);
    } while (n == nAlloc);

    return java.util.Arrays.copyOf(threads, n);
}

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;
            }/* www . j a va  2s  .c o  m*/
        }
    }
    return false;
}

From source file:ThreadTimer.java

public static long getCurrentThreadTime() {
    ThreadMXBean tb = ManagementFactory.getThreadMXBean();
    return tb.getCurrentThreadCpuTime();
}

From source file:Main.java

/**
 * Gets all threads in the JVM.  This is really a snapshot of all threads
 * at the time this method is called./*from w w w . j  a  v a 2s  .  c o m*/
 * @return An array of all threads currently running in the JVM.
 */
static public Thread[] getAllThreads() {
    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    int nAlloc = thbean.getThreadCount();
    int n = 0;
    Thread[] threads;
    do {
        nAlloc *= 2;
        threads = new Thread[nAlloc];
        n = root.enumerate(threads, true);
    } while (n == nAlloc);
    return java.util.Arrays.copyOf(threads, n);
}

From source file:org.commonjava.indy.ftest.core.fixture.ThreadDumper.java

public static void dumpThreads() {
    StringBuilder sb = new StringBuilder();
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
    Stream.of(threadInfos).forEachOrdered((ti) -> {
        if (sb.length() > 0) {
            sb.append("\n\n");
        }//w ww.  j a  v a 2  s. com

        sb.append(ti.getThreadName()).append("\n  State: ").append(ti.getThreadState())
                .append("\n  Lock Info: ").append(ti.getLockInfo()).append("\n  Monitors:");

        MonitorInfo[] monitors = ti.getLockedMonitors();
        if (monitors == null || monitors.length < 1) {
            sb.append("  -NONE-");
        } else {
            sb.append("\n  - ").append(join(monitors, "\n  - "));
        }

        sb.append("\n  Trace:\n    ").append(join(ti.getStackTrace(), "\n    "));

    });

    System.out.println(sb);
}