multithreading - Deadlock detection in Java -
long time ago, saved sentence java reference book: "java has no mechanism handle deadlock. won't know deadlock occurred." (head first java 2nd edition, p.516)
so, it? there way catch deadlock case in java? mean, there way our code understands deadlock case occurred?
since jdk 1.5 there useful methods in java.lang.management
package find , inspect deadlocks occurs. see findmonitordeadlockedthreads()
, finddeadlockedthreads()
method of threadmxbean
class.
a possible way use have separate watchdog thread (or periodic task) this.
sample code:
threadmxbean tmx = managementfactory.getthreadmxbean(); long[] ids = tmx.finddeadlockedthreads(); if (ids != null) { threadinfo[] infos = tmx.getthreadinfo(ids, true, true); system.out.println("the following threads deadlocked:"); (threadinfo ti : infos) { system.out.println(ti); } }
Comments
Post a Comment