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:password.pwm.util.java.JavaHelper.java

/**
 * Copy of {@link ThreadInfo#toString()} but with the MAX_FRAMES changed from 8 to 256.
 *//*  www . ja va 2 s  . c o m*/
public static String threadInfoToString(final ThreadInfo threadInfo) {
    final int MAX_FRAMES = 256;
    final StringBuilder sb = new StringBuilder("\"" + threadInfo.getThreadName() + "\"" + " Id="
            + threadInfo.getThreadId() + " " + threadInfo.getThreadState());
    if (threadInfo.getLockName() != null) {
        sb.append(" on " + threadInfo.getLockName());
    }
    if (threadInfo.getLockOwnerName() != null) {
        sb.append(" owned by \"" + threadInfo.getLockOwnerName() + "\" Id=" + threadInfo.getLockOwnerId());
    }
    if (threadInfo.isSuspended()) {
        sb.append(" (suspended)");
    }
    if (threadInfo.isInNative()) {
        sb.append(" (in native)");
    }
    sb.append('\n');

    int counter = 0;
    for (; counter < threadInfo.getStackTrace().length && counter < MAX_FRAMES; counter++) {
        final StackTraceElement ste = threadInfo.getStackTrace()[counter];
        sb.append("\tat ").append(ste.toString());
        sb.append('\n');
        if (counter == 0 && threadInfo.getLockInfo() != null) {
            final Thread.State ts = threadInfo.getThreadState();
            switch (ts) {
            case BLOCKED:
                sb.append("\t-  blocked on " + threadInfo.getLockInfo());
                sb.append('\n');
                break;
            case WAITING:
                sb.append("\t-  waiting on " + threadInfo.getLockInfo());
                sb.append('\n');
                break;
            case TIMED_WAITING:
                sb.append("\t-  waiting on " + threadInfo.getLockInfo());
                sb.append('\n');
                break;
            default:
            }
        }

        for (MonitorInfo mi : threadInfo.getLockedMonitors()) {
            if (mi.getLockedStackDepth() == counter) {
                sb.append("\t-  locked " + mi);
                sb.append('\n');
            }
        }
    }
    if (counter < threadInfo.getStackTrace().length) {
        sb.append("\t...");
        sb.append('\n');
    }

    final LockInfo[] locks = threadInfo.getLockedSynchronizers();
    if (locks.length > 0) {
        sb.append("\n\tNumber of locked synchronizers = " + locks.length);
        sb.append('\n');
        for (LockInfo li : locks) {
            sb.append("\t- " + li);
            sb.append('\n');
        }
    }
    sb.append('\n');
    return sb.toString();
}

From source file:Main.java

private static void formatState(final StringBuilder sb, final ThreadInfo info) {
    final Thread.State state = info.getThreadState();
    sb.append(state);//from   ww  w.  jav  a 2  s  .c o  m
    switch (state) {
    case BLOCKED: {
        sb.append(" (on object monitor owned by \"");
        sb.append(info.getLockOwnerName()).append("\" Id=").append(info.getLockOwnerId()).append(")");
        break;
    }
    case WAITING: {
        final StackTraceElement element = getStackForThread(info.getThreadId())[0];
        final String className = element.getClassName();
        final String method = element.getMethodName();
        if (className.equals("java.lang.Object") && method.equals("wait")) {
            sb.append(" (on object monitor");
            if (info.getLockOwnerName() != null) {
                sb.append(" owned by \"");
                sb.append(info.getLockOwnerName()).append("\" Id=").append(info.getLockOwnerId());
            }
            sb.append(")");
        } else if (className.equals("java.lang.Thread") && method.equals("join")) {
            sb.append(" (on completion of thread ").append(info.getLockOwnerId()).append(")");
        } else {
            sb.append(" (parking for lock");
            if (info.getLockOwnerName() != null) {
                sb.append(" owned by \"");
                sb.append(info.getLockOwnerName()).append("\" Id=").append(info.getLockOwnerId());
            }
            sb.append(")");
        }
        break;
    }
    case TIMED_WAITING: {
        final StackTraceElement element = info.getStackTrace()[0];
        final String className = element.getClassName();
        final String method = element.getMethodName();
        if (className.equals("java.lang.Object") && method.equals("wait")) {
            sb.append(" (on object monitor");
            if (info.getLockOwnerName() != null) {
                sb.append(" owned by \"");
                sb.append(info.getLockOwnerName()).append("\" Id=").append(info.getLockOwnerId());
            }
            sb.append(")");
        } else if (className.equals("java.lang.Thread") && method.equals("sleep")) {
            sb.append(" (sleeping)");
        } else if (className.equals("java.lang.Thread") && method.equals("join")) {
            sb.append(" (on completion of thread ").append(info.getLockOwnerId()).append(")");
        } else {
            sb.append(" (parking for lock");
            if (info.getLockOwnerName() != null) {
                sb.append(" owned by \"");
                sb.append(info.getLockOwnerName()).append("\" Id=").append(info.getLockOwnerId());
            }
            sb.append(")");
        }
        break;
    }
    default:
        break;
    }
}

From source file:DeadlockDetector.java

private void printThread(ThreadInfo ti) {
    sb.append("\nPrintThread\n");
    sb.append(// w ww. j a  v a2 s .  c  o  m
            "\"" + ti.getThreadName() + "\"" + " Id=" + ti.getThreadId() + " in " + ti.getThreadState() + "\n");
    if (ti.getLockName() != null) {
        sb.append(" on lock=" + ti.getLockName() + "\n");
    }
    if (ti.isSuspended()) {
        sb.append(" (suspended)" + "\n");
    }
    if (ti.isInNative()) {
        sb.append(" (running in native)" + "\n");
    }
    if (ti.getLockOwnerName() != null) {
        sb.append(INDENT + " owned by " + ti.getLockOwnerName() + " Id=" + ti.getLockOwnerId() + "\n");
    }
}

From source file:hudson.Functions.java

@IgnoreJRERequirement
public static String dumpThreadInfo(ThreadInfo ti) {
    StringBuilder sb = new StringBuilder(
            "\"" + ti.getThreadName() + "\"" + " Id=" + ti.getThreadId() + " " + ti.getThreadState());
    if (ti.getLockName() != null) {
        sb.append(" on " + ti.getLockName());
    }//from w  w w. j av  a 2 s .  c o  m
    if (ti.getLockOwnerName() != null) {
        sb.append(" owned by \"" + ti.getLockOwnerName() + "\" Id=" + ti.getLockOwnerId());
    }
    if (ti.isSuspended()) {
        sb.append(" (suspended)");
    }
    if (ti.isInNative()) {
        sb.append(" (in native)");
    }
    sb.append('\n');
    StackTraceElement[] stackTrace = ti.getStackTrace();
    for (int i = 0; i < stackTrace.length; i++) {
        StackTraceElement ste = stackTrace[i];
        sb.append("\tat " + ste.toString());
        sb.append('\n');
        if (i == 0 && ti.getLockInfo() != null) {
            Thread.State ts = ti.getThreadState();
            switch (ts) {
            case BLOCKED:
                sb.append("\t-  blocked on " + ti.getLockInfo());
                sb.append('\n');
                break;
            case WAITING:
                sb.append("\t-  waiting on " + ti.getLockInfo());
                sb.append('\n');
                break;
            case TIMED_WAITING:
                sb.append("\t-  waiting on " + ti.getLockInfo());
                sb.append('\n');
                break;
            default:
            }
        }

        for (MonitorInfo mi : ti.getLockedMonitors()) {
            if (mi.getLockedStackDepth() == i) {
                sb.append("\t-  locked " + mi);
                sb.append('\n');
            }
        }
    }

    LockInfo[] locks = ti.getLockedSynchronizers();
    if (locks.length > 0) {
        sb.append("\n\tNumber of locked synchronizers = " + locks.length);
        sb.append('\n');
        for (LockInfo li : locks) {
            sb.append("\t- " + li);
            sb.append('\n');
        }
    }
    sb.append('\n');
    return sb.toString();
}

From source file:com.streamsets.datacollector.restapi.AdminResource.java

@GET
@Path("/threads")
@ApiOperation(value = "Returns Thread Dump along with stack trace", response = Map.class, responseContainer = "List", authorizations = @Authorization(value = "basic"))
@Produces(MediaType.APPLICATION_JSON)/*  w ww. j av a2s .  c  o m*/
@RolesAllowed({ AuthzRole.ADMIN, AuthzRole.ADMIN_REMOTE })
public Response getThreadsDump() throws IOException {
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo[] threads = threadMXBean.dumpAllThreads(true, true);
    List<Map> augmented = new ArrayList<>(threads.length);
    for (ThreadInfo thread : threads) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("threadInfo", thread);
        map.put("userTimeNanosecs", threadMXBean.getThreadUserTime(thread.getThreadId()));
        map.put("cpuTimeNanosecs", threadMXBean.getThreadCpuTime(thread.getThreadId()));
        augmented.add(map);
    }
    return Response.ok(augmented).build();
}

From source file:com.thoughtworks.go.server.service.support.ThreadInformationProvider.java

private TreeMap<Long, Map<String, Object>> getStackTraceInformation(ThreadMXBean threadMXBean) {
    TreeMap<Long, Map<String, Object>> traces = new TreeMap<>();
    ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true);
    for (ThreadInfo threadInfo : threadInfos) {
        LinkedHashMap<String, Object> threadStackTrace = new LinkedHashMap<>();
        threadStackTrace.put("Id", threadInfo.getThreadId());
        threadStackTrace.put("Name", threadInfo.getThreadName());
        threadStackTrace.put("State", threadInfo.getThreadState());

        LinkedHashMap<String, Object> lockMonitorInfo = new LinkedHashMap<>();
        MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
        ArrayList<Map<String, Object>> lockedMonitorsJson = new ArrayList<>();

        for (MonitorInfo lockedMonitor : lockedMonitors) {
            LinkedHashMap<String, Object> lockedMonitorJson = new LinkedHashMap<>();
            lockedMonitorJson.put("Class", lockedMonitor.getClassName());
            lockedMonitorJson.put("IdentityHashCode", lockedMonitor.getIdentityHashCode());
            lockedMonitorJson.put("LockedStackDepth", lockedMonitor.getLockedStackDepth());
            lockedMonitorJson.put("StackFrame", lockedMonitor.getLockedStackFrame().toString());
            lockedMonitorsJson.add(lockedMonitorJson);
        }//from   ww  w  . j av  a  2s.  c o  m

        lockMonitorInfo.put("Locked Monitors", lockedMonitorsJson);
        lockMonitorInfo.put("Locked Synchronizers", asJSON(threadInfo.getLockedSynchronizers()));
        threadStackTrace.put("Lock Monitor Info", lockMonitorInfo);

        LinkedHashMap<String, Object> blockedInfo = new LinkedHashMap<>();
        blockedInfo.put("Blocked Time", threadInfo.getBlockedTime() == -1 ? null : threadInfo.getBlockedTime());
        blockedInfo.put("Blocked Count", threadInfo.getBlockedCount());
        threadStackTrace.put("Blocked Info", blockedInfo);

        LinkedHashMap<String, Object> timeInfo = new LinkedHashMap<>();
        timeInfo.put("Waited Time", threadInfo.getWaitedTime() == -1 ? null : threadInfo.getWaitedTime());
        timeInfo.put("Waited Count", threadInfo.getWaitedCount());
        threadStackTrace.put("Time Info", timeInfo);

        LinkedHashMap<String, Object> lockInfoMap = new LinkedHashMap<>();
        LockInfo lockInfo = threadInfo.getLockInfo();
        lockInfoMap.put("Locked On", asJSON(lockInfo));
        lockInfoMap.put("Lock Owner Thread Id",
                threadInfo.getLockOwnerId() == -1 ? null : threadInfo.getLockOwnerId());
        lockInfoMap.put("Lock Owner Thread Name", threadInfo.getLockOwnerName());
        threadStackTrace.put("Lock Info", lockInfoMap);

        LinkedHashMap<String, Object> stateInfo = new LinkedHashMap<>();
        stateInfo.put("Suspended", threadInfo.isSuspended());
        stateInfo.put("InNative", threadInfo.isInNative());
        threadStackTrace.put("State Info", stateInfo);

        threadStackTrace.put("Stack Trace", asJSON(threadInfo.getStackTrace()));
        traces.put(threadInfo.getThreadId(), threadStackTrace);
    }
    return traces;
}

From source file:FullThreadDump.java

private void printThread(ThreadInfo ti) {
    StringBuilder sb = new StringBuilder(
            "\"" + ti.getThreadName() + "\"" + " Id=" + ti.getThreadId() + " in " + ti.getThreadState());
    if (ti.getLockName() != null) {
        sb.append(" on lock=" + ti.getLockName());
    }/*from ww w .j a  va2 s. co  m*/
    if (ti.isSuspended()) {
        sb.append(" (suspended)");
    }
    if (ti.isInNative()) {
        sb.append(" (running in native)");
    }
    System.out.println(sb.toString());
    if (ti.getLockOwnerName() != null) {
        System.out.println(INDENT + " owned by " + ti.getLockOwnerName() + " Id=" + ti.getLockOwnerId());
    }
}

From source file:com.streamsets.datacollector.bundles.content.SdcInfoContentGenerator.java

public void threadDump(BundleWriter writer) throws IOException {
    writer.markStartOfFile("runtime/threads.txt");

    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo[] threads = threadMXBean.dumpAllThreads(true, true);

    // Sadly we can't easily do info.toString() as the implementation is hardcoded to cut the stack trace only to 8
    // items which does not serve our purpose well. Hence we have custom implementation that prints entire stack trace
    // for all threads.
    for (ThreadInfo info : threads) {
        StringBuilder sb = new StringBuilder(
                "\"" + info.getThreadName() + "\"" + " Id=" + info.getThreadId() + " " + info.getThreadState());
        if (info.getLockName() != null) {
            sb.append(" on " + info.getLockName());
        }/*  w ww .ja  va  2 s.co  m*/
        if (info.getLockOwnerName() != null) {
            sb.append(" owned by \"" + info.getLockOwnerName() + "\" Id=" + info.getLockOwnerId());
        }
        if (info.isSuspended()) {
            sb.append(" (suspended)");
        }
        if (info.isInNative()) {
            sb.append(" (in native)");
        }
        sb.append('\n');
        int i = 0;
        for (StackTraceElement ste : info.getStackTrace()) {
            if (i == 0 && info.getLockInfo() != null) {
                Thread.State ts = info.getThreadState();
                switch (ts) {
                case BLOCKED:
                    sb.append("\t-  blocked on " + info.getLockInfo());
                    sb.append('\n');
                    break;
                case WAITING:
                    sb.append("\t-  waiting on " + info.getLockInfo());
                    sb.append('\n');
                    break;
                case TIMED_WAITING:
                    sb.append("\t-  waiting on " + info.getLockInfo());
                    sb.append('\n');
                    break;
                default:
                }
            }
            sb.append("\tat " + ste.toString());
            sb.append('\n');

            i++;

            for (MonitorInfo mi : info.getLockedMonitors()) {
                if (mi.getLockedStackDepth() == i) {
                    sb.append("\t-  locked " + mi);
                    sb.append('\n');
                }
            }
        }

        LockInfo[] locks = info.getLockedSynchronizers();
        if (locks.length > 0) {
            sb.append("\n\tNumber of locked synchronizers = " + locks.length);
            sb.append('\n');
            for (LockInfo li : locks) {
                sb.append("\t- " + li);
                sb.append('\n');
            }
        }
        sb.append('\n');

        writer.write(sb.toString());
    }

    writer.markEndOfFile();
}

From source file:edu.usu.sdl.openstorefront.web.rest.service.Application.java

@GET
@RequireAdmin//  ww w. j  a v  a 2 s . c o  m
@APIDescription("Gets the application system thread and status")
@Produces({ MediaType.APPLICATION_JSON })
@DataType(ThreadStatus.class)
@Path("/threads")
public List<ThreadStatus> getThreads() {
    List<ThreadStatus> threadStatuses = new ArrayList<>();

    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo threadInfos[] = threadMXBean.dumpAllThreads(false, false);

    for (ThreadInfo info : threadInfos) {
        ThreadStatus threadStatus = new ThreadStatus();
        threadStatus.setId(info.getThreadId());
        threadStatus.setName(info.getThreadName());
        threadStatus.setStatus(info.getThreadState().name());
        threadStatus
                .setDetails(info.toString().replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;").replace("\n", "<br>"));
        threadStatuses.add(threadStatus);
    }

    return threadStatuses;
}

From source file:org.jahia.tools.jvm.ThreadMonitor.java

private void printThread(ThreadInfo ti, StringBuilder dump) {
    StringBuilder sb = new StringBuilder(
            "\"" + ti.getThreadName() + "\"" + " nid=" + ti.getThreadId() + " state=" + ti.getThreadState());
    if (ti.isSuspended()) {
        sb.append(" (suspended)");
    }/*from ww  w  .j  ava 2  s . co m*/
    if (ti.isInNative()) {
        sb.append(" (running in native)");
    }
    sb.append(" []\n" + Patterns.DOLLAR.matcher(ti.getThreadState().getClass().getName()).replaceAll(".") + ": "
            + ti.getThreadState());
    if (ti.getLockName() != null && ti.getThreadState() != Thread.State.BLOCKED) {
        String[] lockInfo = Patterns.AT.split(ti.getLockName());
        sb.append("\n" + INDENT + "- waiting on <0x" + lockInfo[1] + "> (a " + lockInfo[0] + ")");
        sb.append("\n" + INDENT + "- locked <0x" + lockInfo[1] + "> (a " + lockInfo[0] + ")");
    } else if (ti.getLockName() != null && ti.getThreadState() == Thread.State.BLOCKED) {
        String[] lockInfo = Patterns.AT.split(ti.getLockName());
        sb.append("\n" + INDENT + "- waiting to lock <0x" + lockInfo[1] + "> (a " + lockInfo[0] + ")");
    }
    dump.append(sb.toString());
    dump.append("\n");
    if (ti.getLockOwnerName() != null) {
        dump.append(INDENT + " owned by " + ti.getLockOwnerName() + " id=" + ti.getLockOwnerId());
        dump.append("\n");
    }
}