• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

What is ThreadMXBean in java?

June 14, 2017 By j2eereference Leave a Comment

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.

Java
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 .

Related Posts

  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java

Filed Under: Core Java Tagged With: Deadlock detection, ManagementFactory, ThreadMXBean, ThreadMXBean Interface

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.