Example usage for java.lang.management ThreadInfo toString

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

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representation of this thread info.

Usage

From source file:DeadLockDetector.java

/**
 * Check if there is a DeadLock.//  w  w  w.jav a  2 s.c  o  m
 */
@Override
public final void run() {
    boolean deadlock = false;
    while (!deadlock) {
        try {
            long[] ids = tmx.findDeadlockedThreads();

            if (ids != null) {
                /** deadlock found :/ */
                deadlock = true;
                ThreadInfo[] tis = tmx.getThreadInfo(ids, true, true);
                String info = "DeadLock Found!\n";
                for (ThreadInfo ti : tis) {
                    info += ti.toString();
                }

                for (ThreadInfo ti : tis) {
                    LockInfo[] locks = ti.getLockedSynchronizers();
                    MonitorInfo[] monitors = ti.getLockedMonitors();
                    if (locks.length == 0 && monitors.length == 0) {
                        /** this thread is deadlocked but its not guilty */
                        continue;
                    }

                    ThreadInfo dl = ti;
                    info += "Java-level deadlock:\n";
                    info += "\t" + dl.getThreadName() + " is waiting to lock " + dl.getLockInfo().toString()
                            + " which is held by " + dl.getLockOwnerName() + "\n";
                    while ((dl = tmx.getThreadInfo(new long[] { dl.getLockOwnerId() }, true, true)[0])
                            .getThreadId() != ti.getThreadId()) {
                        info += "\t" + dl.getThreadName() + " is waiting to lock " + dl.getLockInfo().toString()
                                + " which is held by " + dl.getLockOwnerName() + "\n";
                    }
                }
                System.out.println(info);

                if (doWhenDL == RESTART) {
                    System.exit(0);
                }
            }
            Thread.sleep(sleepTime);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

From source file:DeadlockDetector.java

private void printThreadInfo(ThreadInfo threadInfo) {
    printThread(threadInfo);/*from w  w  w .ja  v  a2  s.co  m*/
    sb.append(INDENT + threadInfo.toString() + "\n");
    StackTraceElement[] stacktrace = threadInfo.getStackTrace();
    MonitorInfo[] monitors = threadInfo.getLockedMonitors();

    for (int i = 0; i < stacktrace.length; i++) {
        StackTraceElement ste = stacktrace[i];
        sb.append(INDENT + "at " + ste.toString() + "\n");
        for (MonitorInfo mi : monitors) {
            if (mi.getLockedStackDepth() == i) {
                sb.append(INDENT + "  - locked " + mi + "\n");
            }
        }
    }
}

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

@GET
@RequireAdmin//  w  w w .  ja  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;
}