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: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. jav a  2s. c om
    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:org.apache.synapse.commons.jmx.ThreadingView.java

public int getTotalWorkerCount() {
    int count = 0;
    ThreadInfo[] threadInfo = dumpAllThreads();
    for (ThreadInfo ti : threadInfo) {
        if (ti != null && ti.getThreadName().startsWith(threadNamePrefix)) {
            count++;/*from  w ww.  j ava2s.c o m*/
        }
    }
    return count;
}

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

private Map<String, Object> getDeadLockThreadInformation(ThreadMXBean threadMXBean) {
    LinkedHashMap<String, Object> json = new LinkedHashMap<>();
    long[] deadlockedThreads = threadMXBean.findDeadlockedThreads();

    if (deadlockedThreads != null && deadlockedThreads.length > 0) {
        json.put("Count", deadlockedThreads.length);
        for (long deadlockedThread : deadlockedThreads) {
            LinkedHashMap<String, Object> threadsInfo = new LinkedHashMap<>();
            LinkedHashMap<String, Object> lockedMonitorsInfo = new LinkedHashMap<>();
            LinkedHashMap<String, Object> stackTrackInfo = new LinkedHashMap<>();
            ThreadInfo threadInfo = threadMXBean.getThreadInfo(deadlockedThread);
            LockInfo lockInfo = threadInfo.getLockInfo();
            if (lockInfo != null) {
                threadsInfo.put(threadInfo.getThreadName(), lockInfo);
            } else {
                threadsInfo.put(threadInfo.getThreadName(), "This thread is not waiting for any locks");
            }/*from   w  w w .  j  av a2  s  . com*/
            MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
            for (MonitorInfo lockedMonitor : lockedMonitors) {
                lockedMonitorsInfo.put("Monitor for class " + lockedMonitor.getClassName(),
                        "taken at stack frame " + lockedMonitor.getLockedStackFrame());
            }
            stackTrackInfo.put(Long.toString(deadlockedThread), Arrays.toString(threadInfo.getStackTrace()));
            json.put("Thread Information", threadsInfo);
            json.put("Monitor Information Stack Frame where locks were taken", lockedMonitorsInfo);
            json.put("Stack Trace Of DeadLock Threads", stackTrackInfo);
        }
    }
    return json;
}

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  w ww  . j a va2s.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: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 w w  .j  av  a  2 s  . c o  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:org.apache.synapse.commons.jmx.ThreadingView.java

private double getBlockedWorkerPercentage() {
    int totalCount = 0;
    int blockedCount = 0;
    ThreadInfo[] threadInfo = dumpAllThreads();
    for (ThreadInfo ti : threadInfo) {
        // see if the thread name matches the prefix
        if (ti != null && ti.getThreadName().startsWith(threadNamePrefix)) {
            totalCount++;/*  w w  w  .j  a  va2 s .c  o  m*/
            if (isBlocked(ti)) {
                blockedCount++;
            }
        }
    }
    if (totalCount == 0) {
        return 0;
    }
    return ((double) blockedCount / (double) totalCount) * 100;
}

From source file:org.apache.hadoop.hdfs.server.namenode.ha.TestStandbyCheckpoints.java

/**
 * Test cancellation of ongoing checkpoints when failover happens
 * mid-checkpoint during image upload from standby to active NN.
 *//*from w  ww .j ava2 s.co  m*/
@Test(timeout = 60000)
public void testCheckpointCancellationDuringUpload() throws Exception {
    // don't compress, we want a big image
    cluster.getConfiguration(0).setBoolean(DFSConfigKeys.DFS_IMAGE_COMPRESS_KEY, false);
    cluster.getConfiguration(1).setBoolean(DFSConfigKeys.DFS_IMAGE_COMPRESS_KEY, false);
    // Throttle SBN upload to make it hang during upload to ANN
    cluster.getConfiguration(1).setLong(DFSConfigKeys.DFS_IMAGE_TRANSFER_RATE_KEY, 100);
    cluster.restartNameNode(0);
    cluster.restartNameNode(1);
    nn0 = cluster.getNameNode(0);
    nn1 = cluster.getNameNode(1);

    cluster.transitionToActive(0);

    doEdits(0, 100);
    HATestUtil.waitForStandbyToCatchUp(nn0, nn1);
    HATestUtil.waitForCheckpoint(cluster, 1, ImmutableList.of(104));
    cluster.transitionToStandby(0);
    cluster.transitionToActive(1);

    // Wait to make sure background TransferFsImageUpload thread was cancelled.
    // This needs to be done before the next test in the suite starts, so that a
    // file descriptor is not held open during the next cluster init.
    cluster.shutdown();
    cluster = null;
    GenericTestUtils.waitFor(new Supplier<Boolean>() {
        @Override
        public Boolean get() {
            ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
            ThreadInfo[] threads = threadBean.getThreadInfo(threadBean.getAllThreadIds(), 1);
            for (ThreadInfo thread : threads) {
                if (thread.getThreadName().startsWith("TransferFsImageUpload")) {
                    return false;
                }
            }
            return true;
        }
    }, 1000, 30000);

    // Assert that former active did not accept the canceled checkpoint file.
    assertEquals(0, nn0.getFSImage().getMostRecentCheckpointTxId());
}

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

@GET
@RequireAdmin//from  w  w  w .j  av a  2 s  . com
@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.apache.storm.utils.Utils.java

/**
 * Gets some information, including stack trace, for a running thread.
 * @return A human-readable string of the dump.
 *//*from  w  ww .j a va  2 s.c om*/
public static String threadDump() {
    final StringBuilder dump = new StringBuilder();
    final java.lang.management.ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    final java.lang.management.ThreadInfo[] threadInfos = threadMXBean
            .getThreadInfo(threadMXBean.getAllThreadIds(), 100);
    for (java.lang.management.ThreadInfo threadInfo : threadInfos) {
        dump.append('"');
        dump.append(threadInfo.getThreadName());
        dump.append("\" ");
        dump.append("\n   lock: ");
        dump.append(threadInfo.getLockName());
        dump.append(" owner: ");
        dump.append(threadInfo.getLockOwnerName());
        final Thread.State state = threadInfo.getThreadState();
        dump.append("\n   java.lang.Thread.State: ");
        dump.append(state);
        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.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)");
    }/*  w  ww.ja v a 2s .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");
    }
}