SHARE
TWEET

Untitled

a guest Apr 18th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.lang.management.LockInfo;
  2. import java.lang.management.ManagementFactory;
  3. import java.lang.management.MonitorInfo;
  4. import java.lang.management.ThreadInfo;
  5. import java.lang.management.ThreadMXBean;
  6.  
  7. public class DeadLockDetector extends Thread {
  8.  
  9.     private static final Logger log = LoggerFactory.getLogger(DeadLockDetector.class);
  10.     /** What should we do on DeadLock */
  11.     public static final byte NOTHING = 0;
  12.     /** What should we do on DeadLock */
  13.     public static final byte RESTART = 1;
  14.  
  15.     /** how often check for deadlocks */
  16.     private final int sleepTime;
  17.     /**
  18.      * ThreadMXBean
  19.      */
  20.     private final ThreadMXBean tmx;
  21.     /** What should we do on DeadLock */
  22.     private final byte doWhenDL;
  23.  
  24.     /**
  25.      * Create new DeadLockDetector with given values.
  26.      *
  27.      * @param sleepTime
  28.      * @param doWhenDL
  29.      */
  30.     public DeadLockDetector(final int sleepTime, final byte doWhenDL) {
  31.         super("DeadLockDetector");
  32.         this.sleepTime = sleepTime * 1000;
  33.         this.tmx = ManagementFactory.getThreadMXBean();
  34.         this.doWhenDL = doWhenDL;
  35.     }
  36.  
  37.     /**
  38.      * Check if there is a DeadLock.
  39.      */
  40.     @Override
  41.     public final void run() {
  42.         boolean deadlock = false;
  43.         while (!deadlock)
  44.             try {
  45.                 long[] ids = tmx.findDeadlockedThreads();
  46.  
  47.                 if (ids != null) {
  48.                     /** deadlock found :/ */
  49.                     deadlock = true;
  50.                     ThreadInfo[] tis = tmx.getThreadInfo(ids, true, true);
  51.                     String info = "DeadLock Found!\n";
  52.                     for (ThreadInfo ti : tis)
  53.                         info += ti.toString();
  54.  
  55.                     for (ThreadInfo ti : tis) {
  56.                         LockInfo[] locks = ti.getLockedSynchronizers();
  57.                         MonitorInfo[] monitors = ti.getLockedMonitors();
  58.                         if (locks.length == 0 && monitors.length == 0)
  59.                             /** this thread is deadlocked but its not guilty */
  60.                             continue;
  61.  
  62.                         ThreadInfo dl = ti;
  63.                         info += "Java-level deadlock:\n";
  64.                         info += createShortLockInfo(dl);
  65.                         while ((dl = tmx.getThreadInfo(new long[] { dl.getLockOwnerId() }, true, true)[0]).getThreadId() != ti
  66.                             .getThreadId())
  67.                             info += createShortLockInfo(dl);
  68.  
  69.                         info += "\nDumping all threads:\n";
  70.                         for (ThreadInfo dumpedTI : tmx.dumpAllThreads(true, true)) {
  71.                             info += printDumpedThreadInfo(dumpedTI);
  72.                         }
  73.                     }
  74.                     log.warn(info);
  75.  
  76.                     if (doWhenDL == RESTART)
  77.                         System.exit(ExitCode.CODE_RESTART);
  78.                 }
  79.                 Thread.sleep(sleepTime);
  80.             }
  81.             catch (Exception e) {
  82.                 log.warn("DeadLockDetector: " + e, e);
  83.             }
  84.     }
  85.  
  86.     /**
  87.      * Example:
  88.      * <p>
  89.      * Java-level deadlock:<br>
  90.      * Thread-0 is waiting to lock java.lang.Object@276af2 which is held by main. Locked synchronizers:0 monitors:1<br>
  91.      * main is waiting to lock java.lang.Object@fa3ac1 which is held by Thread-0. Locked synchronizers:0 monitors:1<br>
  92.      * </p>
  93.      */
  94.     private String createShortLockInfo(ThreadInfo threadInfo) {
  95.         StringBuilder sb = new StringBuilder("\t");
  96.         sb.append(threadInfo.getThreadName());
  97.         sb.append(" is waiting to lock ");
  98.         sb.append(threadInfo.getLockInfo().toString());
  99.         sb.append(" which is held by ");
  100.         sb.append(threadInfo.getLockOwnerName());
  101.         sb.append(". Locked synchronizers:");
  102.         sb.append(threadInfo.getLockedSynchronizers().length);
  103.         sb.append(" monitors:");
  104.         sb.append(threadInfo.getLockedMonitors().length);
  105.         sb.append("\n");
  106.         return sb.toString();
  107.     }
  108.  
  109.     /**
  110.      * Full thread info (short info and stacktrace)<br>
  111.      * Example:
  112.      * <p>
  113.      * "Thread-0" Id=10 BLOCKED <br>
  114.      * at com.gamefirst.gameserver.DeadlockTest$1$1.run(DeadlockTest.java:70)<br>
  115.      * - locked java.lang.Object@fa3ac1<br>
  116.      * at java.lang.Thread.run(Thread.java:662)
  117.      * </p>
  118.      */
  119.     private String printDumpedThreadInfo(ThreadInfo threadInfo) {
  120.         StringBuilder sb = new StringBuilder();
  121.         sb.append("\n\"" + threadInfo.getThreadName() + "\"" + " Id=" + threadInfo.getThreadId() + " "
  122.             + threadInfo.getThreadState() + "\n");
  123.         StackTraceElement[] stacktrace = threadInfo.getStackTrace();
  124.         for (int i = 0; i < stacktrace.length; i++) {
  125.             StackTraceElement ste = stacktrace[i];
  126.             sb.append("\t" + "at " + ste.toString() + "\n");
  127.             for (MonitorInfo mi : threadInfo.getLockedMonitors()) {
  128.                 if (mi.getLockedStackDepth() == i) {
  129.                     sb.append("\t-  locked " + mi);
  130.                     sb.append('\n');
  131.                 }
  132.             }
  133.         }
  134.         return sb.toString();
  135.     }
  136. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top