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

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

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -