Example usage for java.lang.management ThreadMXBean isObjectMonitorUsageSupported

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

Introduction

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

Prototype

public boolean isObjectMonitorUsageSupported();

Source Link

Document

Tests if the Java virtual machine supports monitoring of object monitor usage.

Usage

From source file:Main.java

/** Returns all stack traces, including lock info. */
public static String getAllStackTraces() {
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    boolean monitor = threadMXBean.isObjectMonitorUsageSupported();
    boolean sync = threadMXBean.isSynchronizerUsageSupported();
    ThreadInfo[] allThreadInfo = threadMXBean.dumpAllThreads(monitor, sync);
    StringBuilder sb = new StringBuilder("Stack Trace Report:\n");
    buildTrace(allThreadInfo, sb);//from  w ww.  j a v  a  2 s.  c  o m
    return sb.toString();
}

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  av 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./* ww  w  .j av a2  s  .c  om*/
 * 
 * @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

/**
 * 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 w  w .j  a v  a  2  s.  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: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 w w  . j  a  va  2 s  . c  o  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:com.workplacesystems.utilsj.ThreadDumperJdk16.java

@Override
ThreadInfo[] getThreadInfos(ThreadMXBean bean) {
    return bean.dumpAllThreads(bean.isObjectMonitorUsageSupported(), bean.isSynchronizerUsageSupported());
}

From source file:hudson.Functions.java

@IgnoreJRERequirement
public static ThreadInfo[] getThreadInfos() {
    ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
    return mbean.getThreadInfo(mbean.getAllThreadIds(), mbean.isObjectMonitorUsageSupported(),
            mbean.isSynchronizerUsageSupported());
}

From source file:org.ScripterRon.JavaBitcoin.RpcHandler.java

/**
 * Process 'getstacktraces' request//from w w w.jav a2 s  . c o m
 *
 * Response parameters:
 *   locks   - An array of lock objects for locks with waiters
 *   threads - An array of thread objects
 *
 * Lock object:
 *   name   - Lock class name
 *   hash   - Lock identity hash code
 *   thread - Identifier of thread holding the lock
 *
 * Monitor object:
 *   name    - Monitor class name
 *   hash    - Monitor identity hash
 *   depth   - Stack depth where monitor locked
 *   trace   - Stack element where monitor locked
 *
 * Thread object:
 *   blocked - Lock object if thread is waiting on a lock
 *   id      - Thread identifier
 *   locks   - Array of monitor objects for locks held by this thread
 *   name    - Thread name
 *   state   - Thread state
 *   trace   - Array of stack trace elements
 *
 * @return                              Response as a JSONObject
 */
private JSONObject getStackTraces() {
    JSONArray threadsJSON = new JSONArray();
    JSONArray locksJSON = new JSONArray();
    ThreadMXBean tmxBean = ManagementFactory.getThreadMXBean();
    boolean tmxMI = tmxBean.isObjectMonitorUsageSupported();
    ThreadInfo[] tList = tmxBean.dumpAllThreads(tmxMI, false);
    //
    // Generate the response
    //
    for (ThreadInfo tInfo : tList) {
        JSONObject threadJSON = new JSONObject();
        //
        // General thread information
        //
        threadJSON.put("id", tInfo.getThreadId());
        threadJSON.put("name", tInfo.getThreadName());
        threadJSON.put("state", tInfo.getThreadState().toString());
        //
        // Gather lock usage
        //
        if (tmxMI) {
            MonitorInfo[] mList = tInfo.getLockedMonitors();
            if (mList.length > 0) {
                JSONArray monitorsJSON = new JSONArray();
                for (MonitorInfo mInfo : mList) {
                    JSONObject lockJSON = new JSONObject();
                    lockJSON.put("name", mInfo.getClassName());
                    lockJSON.put("hash", mInfo.getIdentityHashCode());
                    lockJSON.put("depth", mInfo.getLockedStackDepth());
                    lockJSON.put("trace", mInfo.getLockedStackFrame().toString());
                    monitorsJSON.add(lockJSON);
                }
                threadJSON.put("locks", monitorsJSON);
            }
            if (tInfo.getThreadState() == Thread.State.BLOCKED) {
                LockInfo lInfo = tInfo.getLockInfo();
                if (lInfo != null) {
                    JSONObject lockJSON = new JSONObject();
                    lockJSON.put("name", lInfo.getClassName());
                    lockJSON.put("hash", lInfo.getIdentityHashCode());
                    lockJSON.put("thread", tInfo.getLockOwnerId());
                    threadJSON.put("blocked", lockJSON);
                    boolean addLock = true;
                    for (Object lock : locksJSON) {
                        if (((String) ((JSONObject) lock).get("name")).equals(lInfo.getClassName())) {
                            addLock = false;
                            break;
                        }
                    }
                    if (addLock)
                        locksJSON.add(lockJSON);
                }
            }
        }
        //
        // Add the stack trace
        //
        StackTraceElement[] elements = tInfo.getStackTrace();
        JSONArray traceJSON = new JSONArray();
        for (StackTraceElement element : elements)
            traceJSON.add(element.toString());
        threadJSON.put("trace", traceJSON);
        //
        // Add the thread to the response
        //
        threadsJSON.add(threadJSON);
    }
    //
    // Return the response
    //
    JSONObject response = new JSONObject();
    response.put("threads", threadsJSON);
    response.put("locks", locksJSON);
    return response;
}