Example usage for java.lang.management ThreadInfo getLockOwnerName

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

Introduction

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

Prototype

public String getLockOwnerName() 

Source Link

Document

Returns the name of the thread which owns the object for which the thread associated with this ThreadInfo is blocked waiting.

Usage

From source file:DeadLockDetector.java

/**
 * Check if there is a DeadLock.//from  ww  w.j  a  v  a2  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:com.buaa.cfs.utils.ReflectionUtils.java

/**
 * Print all of the thread's information and stack traces.
 *
 * @param stream the stream to/*from  w  w  w.ja va2s. c  om*/
 * @param title  a string title for the stack trace
 */
public synchronized static void printThreadInfo(PrintStream 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();
}

From source file:com.alicloud.tablestore.adaptor.client.IntegratedTest.java

public static void printThreadInfo() {
    String title = "Automatic Stack Trace";
    PrintWriter stream = new PrintWriter(System.out);
    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, 20);
        if (info == null) {
            stream.println("  Inactive");
        } else {/*w  w  w  .  ja v a2  s  .c  o  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: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());
    }//  www  .  j  a v  a  2s .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:DeadlockDetector.java

private void printThread(ThreadInfo ti) {
    sb.append("\nPrintThread\n");
    sb.append(//w  w w.  j a  va2  s  . co 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: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());
        }//from  w  w  w . jav a2s .c om
        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: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  w  ww.ja va  2s.c  o  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.workplacesystems.utilsj.ThreadDumperJdk16.java

@Override
void outputWaitingInfo(ThreadInfo info, ExtraLockInfo exLockInfo, StringBuffer buffer) {
    final LockInfo lock_info = info.getLockInfo();
    if (lock_info != null) {
        formatLock(lock_info, "waiting on", buffer);
        if (exLockInfo != null) {
            if (exLockInfo.isWaitingFor(ExtraLockInfo.WaitingFor.WRITE, lock_info))
                buffer.append(" for write");
            if (exLockInfo.isWaitingFor(ExtraLockInfo.WaitingFor.READ, lock_info))
                buffer.append(" for read");
        }//from www.  j  a  va2s.  c  o  m
        buffer.append(".");
        long lock_owner = info.getLockOwnerId();
        if (lock_owner != -1) {
            buffer.append(" Locked by \"");
            buffer.append(info.getLockOwnerName());
            buffer.append("\" tid=");
            buffer.append(lock_owner);
        }
        buffer.append("\n");
    }
}

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   www.  j  av  a 2 s.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: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 w w w .jav  a 2  s .  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());
    }
}