static String buildDeadlockInfo() {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    long[] threadIds = threadBean.findMonitorDeadlockedThreads();
    if (threadIds != null && threadIds.length > 0) {
      StringWriter stringWriter = new StringWriter();
      PrintWriter out = new PrintWriter(stringWriter);

      ThreadInfo[] infos = threadBean.getThreadInfo(threadIds, true, true);
      for (ThreadInfo ti : infos) {
        printThreadInfo(ti, out);
        printLockInfo(ti.getLockedSynchronizers(), out);
        out.println();
      }

      out.close();
      return stringWriter.toString();
    } else {
      return null;
    }
  }
JUnit run listener which prints full thread dump into System.err in case a test is failed due to
timeout.
hbase-common/src/test/java/org/apache/hadoop/hbase/TimedOutTestsListener.java