@Override
  public void run() {
    while (true) {
      try {
        Thread.sleep(SLEEP_TIME_MS);
      } catch (InterruptedException ex) {
      }
      long[] ids = mbean.findMonitorDeadlockedThreads();
      StringBuilder errBuilder = new StringBuilder();
      if (ids != null && ids.length > 0) {
          errBuilder.append(getThreadsStacktraces(Arrays.asList(ArrayUtils.toObject(ids))));
          errorMessages.add(errBuilder.toString());
          System.out.append(errBuilder.toString());
         //Exit if deadlocks have been found         
          deadlocked = true;
          break;
      } else {
        //Exit if all monitored threads were finished
        boolean hasLive = false;
        boolean hasRunning = false;
        for (Thread monTh : monitoredThreads) {
          State state = monTh.getState();
          if (state != State.TERMINATED && state != State.NEW) {
            hasLive = true;
          }
          if (state == State.RUNNABLE || state == State.TIMED_WAITING) {
            hasRunning = true;
            break;
          }
        }

        if (!hasLive) {
          deadlocked = false;
          break;
        } else if (!hasRunning) {
          List<Long> tIds = new ArrayList<>();
          for (Thread monitoredThread : monitoredThreads) {
            State state = monitoredThread.getState();
            if (state == State.WAITING || state == State.BLOCKED) {
              tIds.add(monitoredThread.getId());
            }
          }
          errBuilder.append(getThreadsStacktraces(tIds));
          errorMessages.add(errBuilder.toString());
          deadlocked = true;
          break;
        }
      }
    }
  }
Monitoring of deadlocks thread
Please note. This class can not be used outside of tests
ambari-server/src/test/java/org/apache/ambari/server/testing/DeadlockWarningThread.java