Example usage for java.lang.management ThreadInfo getLockedSynchronizers

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

Introduction

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

Prototype

public LockInfo[] getLockedSynchronizers() 

Source Link

Document

Returns an array of LockInfo objects, each of which represents an ownable synchronizer currently locked by the thread associated with this ThreadInfo .

Usage

From source file:Main.java

/** Add locked synchronizers data. */
private static void addLockedSynchronizers(ThreadInfo info, StringBuilder sb) {
    LockInfo[] lockInfo = info.getLockedSynchronizers();
    if (lockInfo.length > 0) {
        sb.append("\n\tNumber of locked synchronizers = " + lockInfo.length + "\n");
        for (int i = 0; i < lockInfo.length; i++) {
            sb.append("\t- " + lockInfo[i] + "\n");
        }/*w  w w .jav  a2  s  .com*/
    }
}

From source file:Main.java

private static String threadLockedSynchronizers(ThreadInfo threadInfo) {
    final String NO_SYNCH_INFO = "no locked synchronizers information available\n";
    if (null == threadInfo) {
        return NO_SYNCH_INFO;
    }/*w ww.  j a v  a 2 s  . c  om*/
    try {
        final LockInfo[] lockInfos = threadInfo.getLockedSynchronizers();
        if (lockInfos.length > 0) {
            final StringBuffer lockedSynchBuff = new StringBuffer();
            lockedSynchBuff.append("\nLocked Synchronizers: \n");
            for (final LockInfo lockInfo : lockInfos) {
                lockedSynchBuff.append(lockInfo.getClassName()).append(" <")
                        .append(lockInfo.getIdentityHashCode()).append("> \n");
            }
            return lockedSynchBuff.append("\n").toString();
        } else {
            return "";
        }
    } catch (final Exception e) {
        return NO_SYNCH_INFO;
    }
}

From source file:com.l2jfree.lang.L2Thread.java

public static List<String> getStats(Thread t) {
    List<String> list = new FastList<String>();

    list.add(t.toString() + " - ID: " + t.getId());
    list.add(" * State: " + t.getState());
    list.add(" * Alive: " + t.isAlive());
    list.add(" * Daemon: " + t.isDaemon());
    list.add(" * Interrupted: " + t.isInterrupted());
    for (ThreadInfo info : ManagementFactory.getThreadMXBean().getThreadInfo(new long[] { t.getId() }, true,
            true)) {//from   ww  w.j  a va  2 s.c  o m
        for (MonitorInfo monitorInfo : info.getLockedMonitors()) {
            list.add("==========");
            list.add(" * Locked monitor: " + monitorInfo);
            list.add("\t[" + monitorInfo.getLockedStackDepth() + ".]: at " + monitorInfo.getLockedStackFrame());
        }

        for (LockInfo lockInfo : info.getLockedSynchronizers()) {
            list.add("==========");
            list.add(" * Locked synchronizer: " + lockInfo);
        }

        list.add("==========");
        for (StackTraceElement trace : info.getStackTrace())
            list.add("\tat " + trace);
    }

    return list;
}

From source file:DeadlockDetector.java

@Override
public void run() {
    boolean noDeadLocks = true;

    while (noDeadLocks) {
        try {/*  w  w w.ja v  a  2  s  .  co  m*/
            ThreadMXBean bean = ManagementFactory.getThreadMXBean();
            long[] threadIds = bean.findDeadlockedThreads();

            if (threadIds != null) {
                System.out.println("Deadlock detected!");
                sb = new StringBuilder();
                noDeadLocks = false;

                ThreadInfo[] infos = bean.getThreadInfo(threadIds);
                sb.append("\nTHREAD LOCK INFO: \n");
                for (ThreadInfo threadInfo : infos) {
                    printThreadInfo(threadInfo);
                    LockInfo[] lockInfos = threadInfo.getLockedSynchronizers();
                    MonitorInfo[] monitorInfos = threadInfo.getLockedMonitors();

                    printLockInfo(lockInfos);
                    printMonitorInfo(threadInfo, monitorInfos);
                }

                sb.append("\nTHREAD DUMPS: \n");
                for (ThreadInfo ti : bean.dumpAllThreads(true, true)) {
                    printThreadInfo(ti);
                }
                System.out.println(sb.toString());
            }
            Thread.sleep(checkInterval);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

From source file:com.workplacesystems.utilsj.ThreadDumperJdk16.java

@Override
void outputLockedSynchronizers(ThreadInfo info, ExtraLockInfo exLockInfo, StringBuffer buffer) {
    List<LockInfo> locked_synchronizers = Arrays.asList(info.getLockedSynchronizers());
    if (!locked_synchronizers.isEmpty() || (exLockInfo != null && exLockInfo.hasHeldLocks())) {
        buffer.append("   Locked Synchronizers:\n");
        ArrayList<String> reportedSyncs = new ArrayList<String>();
        for (LockInfo lockInfo : locked_synchronizers) {
            reportedSyncs.add(lockInfo.toString());
            formatLock(lockInfo, null, buffer);
            if (exLockInfo != null) {
                if (exLockInfo.heldWritesContains(lockInfo))
                    buffer.append(" for write");
                if (exLockInfo.heldReadsContains(lockInfo))
                    buffer.append(" for read");
            }/*from   www  .  j  a  v a2 s.com*/
            buffer.append("\n");
        }

        if (exLockInfo != null) {
            for (LockInfo writeLock : exLockInfo.getHeldWriteLocks()) {
                if (!reportedSyncs.contains(writeLock.toString())) {
                    formatLock(writeLock, null, buffer);
                    buffer.append(" for write\n");
                }
            }
            for (LockInfo readLock : exLockInfo.getHeldReadLocks()) {
                if (!reportedSyncs.contains(readLock.toString())) {
                    formatLock(readLock, null, buffer);
                    buffer.append(" for read\n");
                }
            }
        }
        buffer.append("\n");
    }
}

From source file:DeadLockDetector.java

/**
 * Check if there is a DeadLock.// w w  w. ja va2  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: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 a2 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:FullThreadDump.java

/**
 * Prints the thread dump information with locks info to System.out.
 */// w ww .  ja v  a2 s  .c om
private void dumpThreadInfoWithLocks() {
    System.out.println("Full Java thread dump with locks info");

    ThreadInfo[] tinfos = tmbean.dumpAllThreads(true, true);
    for (ThreadInfo ti : tinfos) {
        printThreadInfo(ti);
        LockInfo[] syncs = ti.getLockedSynchronizers();
        printLockInfo(syncs);
    }
    System.out.println();
}

From source file:Main.java

public static String[] getStackTrace(long threadID) {
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo threadInfo = threadMXBean.getThreadInfo(threadID, Integer.MAX_VALUE);

    List<String> buf = new ArrayList<String>();

    if (threadInfo == null) {
        return buf.toArray((String[]) Array.newInstance(String.class, buf.size()));
    }//w w w. j a  v  a2 s. c o m

    StackTraceElement[] stackTrace = threadInfo.getStackTrace();
    for (int i = 0; i < stackTrace.length; i++) {
        StackTraceElement ste = stackTrace[i];
        buf.add("\t" + ste.toString());

        if (i == 0 && threadInfo.getLockInfo() != null) {
            Thread.State ts = threadInfo.getThreadState();
            switch (ts) {
            case BLOCKED:
                buf.add("\t-  blocked on " + threadInfo.getLockInfo());
                break;
            case WAITING:
                buf.add("\t-  waiting on " + threadInfo.getLockInfo());
                break;
            case TIMED_WAITING:
                buf.add("\t-  waiting on " + threadInfo.getLockInfo());
                break;
            default:
            }
        }

        for (MonitorInfo mi : threadInfo.getLockedMonitors()) {
            if (mi.getLockedStackDepth() == i) {
                buf.add("\t-  locked " + mi);
            }
        }
    }

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

    return buf.toArray((String[]) Array.newInstance(String.class, buf.size()));
}

From source file:FullThreadDump.java

/**
 * Checks if any threads are deadlocked. If any, print the thread dump
 * information.//from   w  w w . j  a v  a 2 s  . c o  m
 */
public boolean findDeadlock() {
    long[] tids;
    if (findDeadlocksMethodName.equals("findDeadlockedThreads") && tmbean.isSynchronizerUsageSupported()) {
        tids = tmbean.findDeadlockedThreads();
        if (tids == null) {
            return false;
        }

        System.out.println("Deadlock found :-");
        ThreadInfo[] infos = tmbean.getThreadInfo(tids, true, true);
        for (ThreadInfo ti : infos) {
            printThreadInfo(ti);
            printLockInfo(ti.getLockedSynchronizers());
            System.out.println();
        }
    } else {
        tids = tmbean.findMonitorDeadlockedThreads();
        if (tids == null) {
            return false;
        }
        ThreadInfo[] infos = tmbean.getThreadInfo(tids, Integer.MAX_VALUE);
        for (ThreadInfo ti : infos) {
            // print thread information
            printThreadInfo(ti);
        }
    }

    return true;
}