Example usage for java.lang.management ThreadInfo getThreadName

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

Introduction

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

Prototype

public String getThreadName() 

Source Link

Document

Returns the name of the thread associated with this ThreadInfo .

Usage

From source file:Main.java

public static String gerThreadDump(Thread thread) {

    Preconditions.checkNotNull(thread);//from  www  .ja  v a2  s .c o  m

    StringBuffer dump = new StringBuffer(200);

    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

    ThreadInfo threadInfo = threadMXBean.getThreadInfo(thread.getId(), 100);

    dump.append(lineSeperator);

    dump.append('"');
    dump.append(threadInfo.getThreadName());
    dump.append("\" ");
    final Thread.State state = threadInfo.getThreadState();
    dump.append(lineSeperator);
    dump.append("   State: ");
    dump.append(state);
    final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
    for (final StackTraceElement stackTraceElement : stackTraceElements) {
        dump.append(lineSeperator);
        dump.append("        at ");
        dump.append(stackTraceElement);
    }
    dump.append(lineSeperator);

    return dump.toString();
}

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;
            }//from   w  w w . j  a va 2s  .  com
        }
    }
    return false;
}

From source file:org.apache.metron.common.utils.ErrorUtils.java

public static String generateThreadDump() {
    final StringBuilder dump = new StringBuilder();
    final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    final ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
    for (ThreadInfo threadInfo : threadInfos) {
        dump.append('"');
        dump.append(threadInfo.getThreadName());
        dump.append("\" ");
        final Thread.State state = threadInfo.getThreadState();
        dump.append("\n   java.lang.Thread.State: ");
        dump.append(state);//from   w  ww.jav a  2 s. c  o m
        final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
        for (final StackTraceElement stackTraceElement : stackTraceElements) {
            dump.append("\n        at ");
            dump.append(stackTraceElement);
        }
        dump.append("\n\n");
    }
    return dump.toString();
}

From source file:org.apache.bookkeeper.common.testing.util.TimedOutTestsListener.java

private static void printThread(ThreadInfo ti, PrintWriter out) {
    out.print("\"" + ti.getThreadName() + "\"" + " Id=" + ti.getThreadId() + " in " + ti.getThreadState());
    if (ti.getLockName() != null) {
        out.print(" on lock=" + ti.getLockName());
    }//from www  . ja v  a  2s  .  c o m
    if (ti.isSuspended()) {
        out.print(" (suspended)");
    }
    if (ti.isInNative()) {
        out.print(" (running in native)");
    }
    out.println();
    if (ti.getLockOwnerName() != null) {
        out.println(indent + " owned by " + ti.getLockOwnerName() + " Id=" + ti.getLockOwnerId());
    }
}

From source file:Main.java

public static String takeDumpJSON(ThreadMXBean threadMXBean) throws IOException {
    ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true);
    List<Map<String, Object>> threads = new ArrayList<>();

    for (ThreadInfo thread : threadInfos) {
        Map<String, Object> threadMap = new HashMap<>();
        threadMap.put("name", thread.getThreadName());
        threadMap.put("id", thread.getThreadId());
        threadMap.put("state", thread.getThreadState().name());
        List<String> stacktrace = new ArrayList<>();
        for (StackTraceElement element : thread.getStackTrace()) {
            stacktrace.add(element.toString());
        }/* www.  j  a v a 2 s. c  o m*/
        threadMap.put("stack", stacktrace);

        if (thread.getLockName() != null) {
            threadMap.put("lock_name", thread.getLockName());
        }
        if (thread.getLockOwnerId() != -1) {
            threadMap.put("lock_owner_id", thread.getLockOwnerId());
        }
        if (thread.getBlockedTime() > 0) {
            threadMap.put("blocked_time", thread.getBlockedTime());
        }
        if (thread.getBlockedCount() > 0) {
            threadMap.put("blocked_count", thread.getBlockedCount());
        }
        if (thread.getLockedMonitors().length > 0) {
            threadMap.put("locked_monitors", thread.getLockedMonitors());
        }
        if (thread.getLockedSynchronizers().length > 0) {
            threadMap.put("locked_synchronizers", thread.getLockedSynchronizers());
        }
        threads.add(threadMap);
    }
    ObjectMapper om = new ObjectMapper();
    ObjectNode json = om.createObjectNode();
    json.put("date", new Date().toString());
    json.putPOJO("threads", threads);

    long[] deadlockedThreads = threadMXBean.findDeadlockedThreads();
    long[] monitorDeadlockedThreads = threadMXBean.findMonitorDeadlockedThreads();
    if (deadlockedThreads != null && deadlockedThreads.length > 0) {
        json.putPOJO("deadlocked_thread_ids", deadlockedThreads);
    }
    if (monitorDeadlockedThreads != null && monitorDeadlockedThreads.length > 0) {
        json.putPOJO("monitor_deadlocked_thread_ids", monitorDeadlockedThreads);
    }
    om.enable(SerializationFeature.INDENT_OUTPUT);
    return om.writerWithDefaultPrettyPrinter().writeValueAsString(json);
}

From source file:com.microsoft.sqlserver.jdbc.connection.PoolingTest.java

/**
 * count number of mssql-jdbc-TimeoutTimer threads
 * // www .  ja  v  a  2 s. c  o  m
 * @return
 */
private static int countTimeoutThreads() {
    int count = 0;
    String threadName = "mssql-jdbc-TimeoutTimer";

    ThreadInfo[] tinfos = ManagementFactory.getThreadMXBean()
            .getThreadInfo(ManagementFactory.getThreadMXBean().getAllThreadIds(), 0);

    for (ThreadInfo ti : tinfos) {
        if ((ti.getThreadName().startsWith(threadName))
                && (ti.getThreadState().equals(java.lang.Thread.State.TIMED_WAITING))) {
            count++;
        }
    }

    return count;
}

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  ww w.j a v a 2  s. c o 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:org.alfresco.extension.bulkimport.util.LogUtils.java

public final static String dumpThread(final String threadName) {
    final StringBuilder result = new StringBuilder();
    final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    final ThreadInfo[] threadsInfo = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);

    for (final ThreadInfo threadInfo : threadsInfo) {
        if (threadName == null || threadName.equals(threadInfo.getThreadName())) {
            result.append("\nName: ");
            result.append(threadInfo.getThreadName());
            result.append("\nState: ");
            result.append(threadInfo.getThreadState());
            result.append("\nStack Trace:");

            for (final StackTraceElement stackTraceElement : threadInfo.getStackTrace()) {
                result.append("\n\t\tat ");
                result.append(stackTraceElement);
            }//from  ww  w. j  a v  a  2  s . c  o  m
        }
    }

    return (result.toString());
}

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;
        }//from  ww w  .j  a v  a2s .co 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();
}

From source file:ReflectionUtils.java

/**
 * Print all of the thread's information and stack traces.
 * //from  w w w .  j  av a2  s.c  om
 * @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();
}