Example usage for java.lang.management ThreadInfo getThreadId

List of usage examples for java.lang.management ThreadInfo getThreadId

Introduction

In this page you can find the example usage for java.lang.management ThreadInfo getThreadId.

Prototype

public long getThreadId() 

Source Link

Document

Returns the ID of the thread associated with this ThreadInfo .

Usage

From source file:Main.java

public static void printThreadInfo(ThreadInfo info, StringBuilder builder) {
    builder.append("Thread ").append(info.getThreadName()).append("\" (").append(info.getThreadId())
            .append(") -- ");
    formatState(builder, info);//from w w w  .java2 s  .co m
    if (info.isSuspended()) {
        builder.append(" (suspended)");
    }
    if (info.isInNative()) {
        builder.append(" (in native)");
    }
    builder.append("\n");

    // The waited lock.
    LockInfo li = info.getLockInfo();
    if (li != null) {
        builder.append("Waiting for ");
        formatLock(builder, li);
        if (info.getLockOwnerName() != null) {
            builder.append(" owned by ").append(info.getLockOwnerName()).append("(")
                    .append(info.getLockOwnerId()).append(")");
        }
        builder.append("\n");
    }

    // The stack
    builder.append("Stack:\n");
    printStack(info, builder, getStackForThread(info.getThreadId()));
}

From source file:Main.java

/**
 * Get the thread for the given thread info.  A null
 * is returned if the thread cannot be found.
 *
 * @param   info   the thread info to search for
 * @return      the thread, or null if not found
 * @throws   NullPointerException//  w w  w .  j a  v  a2 s  . com
 *          if info is null
 */
public static Thread getThread(final ThreadInfo info) {
    if (info == null)
        throw new NullPointerException("Null info");
    return getThread(info.getThreadId());
}

From source file:ReflectionUtils.java

/**
 * Print all of the thread's information and stack traces.
 * // www  .  j a v a 2  s.c o m
 * @param stream
 *          the stream to
 * @param title
 *          a string title for the stack trace
 */
public static void printThreadInfo(PrintWriter stream, String title) {
    final int STACK_DEPTH = 20;
    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;
        }
        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:Main.java

/**
 * Get the thread for the given thread info. A null is returned if the
 * thread cannot be found./*from   w w  w . j a  va2 s  . com*/
 * 
 * @param info
 *        the thread info to search for
 * @return the thread, or null if not found
 * @throws NullPointerException
 *         if info is null
 */
public static Thread getThread(final ThreadInfo info) {
    if (info == null) {
        throw new NullPointerException("Null info");
    }
    return getThread(info.getThreadId());
}

From source file:org.apache.hadoop.hbase.util.ReflectionUtils.java

/**
 * Print all of the thread's information and stack traces.
 *
 * @param stream the stream to//  w  w  w .  j  a  va  2s  .com
 * @param title a string title for the stack trace
 */
private static void printThreadInfo(PrintStream stream, String title) {
    final int STACK_DEPTH = 20;
    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;
        }
        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:org.apache.hama.util.ReflectionUtils.java

/**
 * Print all of the thread's information and stack traces.
 * /*  ww w  .  j ava  2  s  . c o  m*/
 * @param stream the stream to
 * @param title a string title for the stack trace
 */
public synchronized static void printThreadInfo(PrintWriter stream, String title) {
    final int STACK_DEPTH = 20;
    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;
        }
        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:EventDispatchThreadHangMonitor.java

private static void checkForDeadlock() {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    long[] threadIds = threadBean.findMonitorDeadlockedThreads();
    if (threadIds == null) {
        return;// w  ww  .  j a va  2  s .co  m
    }
    Log.warn("deadlock detected involving the following threads:");
    ThreadInfo[] threadInfos = threadBean.getThreadInfo(threadIds, Integer.MAX_VALUE);
    for (ThreadInfo info : threadInfos) {
        Log.warn("Thread #" + info.getThreadId() + " " + info.getThreadName() + " (" + info.getThreadState()
                + ") waiting on " + info.getLockName() + " held by " + info.getLockOwnerName()
                + stackTraceToString(info.getStackTrace()));
    }
}

From source file:org.apache.hadoop.util.ReflectionUtils.java

/**
 * Print all of the thread's information and stack traces.
 *
 * @param stream the stream to//from w  w  w.j  a va 2 s .  c  o  m
 * @param title a string title for the stack trace
 */
public static synchronized void printThreadInfo(PrintWriter stream, String title) {
    final int stackDepth = 20;
    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, stackDepth);
        if (info == null) {
            stream.println("  Inactive");
            continue;
        }
        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.buaa.cfs.utils.ReflectionUtils.java

/**
 * Print all of the thread's information and stack traces.
 *
 * @param stream the stream to//from  w w  w.  j  a  va2 s  .  c  o m
 * @param title  a string title for the stack trace
 */
public synchronized static void printThreadInfo(PrintStream stream, String title) {
    final int STACK_DEPTH = 20;
    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;
        }
        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.alicloud.tablestore.adaptor.client.IntegratedTest.java

public static void printThreadInfo() {
    String title = "Automatic Stack Trace";
    PrintWriter stream = new PrintWriter(System.out);
    int STACK_DEPTH = 20;
    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, 20);
        if (info == null) {
            stream.println("  Inactive");
        } else {/*from   w  ww. java  2  s. c  o m*/
            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();
}