PageRenderTime 3005ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/DeadLockDetector.java

http://github.com/traitor/Minecraft-Server-Mod
Java | 74 lines | 47 code | 9 blank | 18 comment | 11 complexity | c9c1e0f52d8595cf1c7ad04c57dcfbb4 MD5 | raw file
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. import java.lang.management.LockInfo;
  16. import java.lang.management.ManagementFactory;
  17. import java.lang.management.MonitorInfo;
  18. import java.lang.management.ThreadInfo;
  19. import java.lang.management.ThreadMXBean;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. /**
  23. * @author -Nemesiss- L2M
  24. *
  25. */
  26. public class DeadLockDetector extends Thread {
  27. private static Logger _log = Logger.getLogger("Minecraft");
  28. private static final int _sleepTime = 5 * 1000;
  29. private final ThreadMXBean tmx;
  30. public DeadLockDetector() {
  31. super("DeadLockDetector");
  32. tmx = ManagementFactory.getThreadMXBean();
  33. _log.info("hMod: Deadlock Detector Thread initialized.");
  34. }
  35. @Override
  36. public final void run() {
  37. boolean deadlock = false;
  38. while (!deadlock)
  39. try {
  40. long[] ids = tmx.findDeadlockedThreads();
  41. if (ids != null) {
  42. deadlock = true;
  43. ThreadInfo[] tis = tmx.getThreadInfo(ids, true, true);
  44. String info = "DeadLock Found!\n";
  45. for (ThreadInfo ti : tis)
  46. info += ti.toString();
  47. for (ThreadInfo ti : tis) {
  48. LockInfo[] locks = ti.getLockedSynchronizers();
  49. MonitorInfo[] monitors = ti.getLockedMonitors();
  50. if (locks.length == 0 && monitors.length == 0)
  51. continue;
  52. ThreadInfo dl = ti;
  53. info += "Java-level deadlock:\n";
  54. info += "\t" + dl.getThreadName() + " is waiting to lock " + dl.getLockInfo().toString() + " which is held by " + dl.getLockOwnerName() + "\n";
  55. while ((dl = tmx.getThreadInfo(new long[] { dl.getLockOwnerId() }, true, true)[0]).getThreadId() != ti.getThreadId())
  56. info += "\t" + dl.getThreadName() + " is waiting to lock " + dl.getLockInfo().toString() + " which is held by " + dl.getLockOwnerName() + "\n";
  57. }
  58. _log.warning(info);
  59. }
  60. Thread.sleep(_sleepTime);
  61. } catch (Exception e) {
  62. _log.log(Level.WARNING, "DeadLockDetector: ", e);
  63. }
  64. }
  65. }