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

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -