What is ThreadMXBean :
In the previous article, we have explained about deadlock, why it occurs and how to avoid deadlock condition. Here we will see what is ThreadMXBean and how can it detect deadlock threads. ThreadMXBean is an interface and belongs to java.lang.Management package. It helps to detect the threads which have entered into deadlock condition and also helps in retrieving details about them.
ThreadMXBean is an interface, to get an instance of ThreadMXBean use getThreadMXBean() method of ManagementFactory.
Let’s create a program to explain some methods of ThreadMXBean interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
package com.j2eereference; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; import java.lang.management.ThreadMXBean; class ThreadDemo { synchronized void executeMethod1(ThreadDemo demo) { System.out.println("Thread "+Thread.currentThread().getName()+" is executing"); demo.executeMethod2(this); System.out.println(Thread.currentThread().getName()+" has finished method execution"); } synchronized void executeMethod2(ThreadDemo demo) { System.out.println("Thread "+Thread.currentThread().getName()+" is executing"); demo.executeMethod1(this); System.out.println(Thread.currentThread().getName()+" has finished method execution"); } } public class ThreadsMXBeanDemo { public static void main(String[] args) { final ThreadDemo threadDemo1 = new ThreadDemo(); final ThreadDemo threadDemo2 = new ThreadDemo(); Thread t1 = new Thread("Thread1") { public void run() { threadDemo1.executeMethod1(threadDemo2); } }; Thread t2 = new Thread("Thread2") { @Override public void run() { threadDemo2.executeMethod2(threadDemo1); } }; t1.start(); t2.start(); ThreadMXBean mxBean = ManagementFactory.getThreadMXBean(); long threadId[] = mxBean.findMonitorDeadlockedThreads(); if(threadId != null) { ThreadInfo info[] = mxBean.getThreadInfo(threadId); for (ThreadInfo threadInfo : info) { System.out.println("\nDeadlock Thread details."); System.out.println(threadInfo.getThreadId()); System.out.println(threadInfo.getThreadName()); System.out.println(threadInfo.getLockName()); System.out.println(threadInfo.getLockOwnerId()); System.out.println(threadInfo.getLockOwnerName()); } } System.out.println("\nGeneral Thread details"); System.out.println(("Current number of live threads :"+ mxBean.getThreadCount())); System.out.println("Total CPU time for the current thread: "+mxBean.getCurrentThreadCpuTime()); System.out.println("Current number of live daemon threads:"+mxBean.getDaemonThreadCount()); System.out.println("Peak live thread count :"+mxBean.getPeakThreadCount()); System.out.println("Total number of threads created and started" +mxBean.getTotalStartedThreadCount()); } } |
OutPut :
Thread Thread1 is executing
Thread Thread2 is executing
Deadlock Thread details.
11
Thread2
com.j2eereference.ThreadDemo@15db9742
10
Thread1
Deadlock Thread details.
10
Thread1
com.j2eereference.ThreadDemo@6d06d69c
11
Thread2
General Thread details
Current number of live threads :7
Total CPU time for the current thread: 46800300
Current number of live daemon threads:4
Peak live thread count :7
Total number of threads created and started7
Important methods of ThreadMXBean:
findMonitorDeadlockedThreads(): This method will return number threads that are in deadlock state and waiting to acquire object monitors.
getThreadId(): This method will return the unique ID of thread
getThreadName(): This method will return the thread name
getLockName(): this method will reurn the string representation of an object for which associated thread is blocked waiting.
getLockOwnerId(): This method will return the ID of the thread which owns the object lock
getLockOwnerName(): This method will return the the name of the thread which owns the object lock.
getThreadCount(): This method will return number of live threads including both daemon and non-daemon threads.
getCurrentThreadCpuTime(): This method will return total CPU time for the current thread in
getDaemonThreadCount(): This method will return current number of live daemon threads.
getPeakThreadCount(): This method will return the peak live thread count since the Java virtual machine has been started .
Leave a Reply