java.lang.management.ThreadInfo Class in Java - GeeksforGeeks

03 Sep, 2021
java.lang.management.ThreadInfo class contains methods to get information about a thread. This information includes:
Thread ID
Thread Name
State of the Thread
Stack trace of the Thread
The object upon which the Thread is blocked
List of object monitors that are blocked by the Thread
List of ownable synchronizers blocked by the Thread
Number of times the Thread had been blocked
The elapsed time for which the Thread had been blocked
Syntax: Class declaration
public class ThreadInfo
extends Object
The methods of this class are as follows: 
Implementation:
Example 1: Creating a new ThreadInfo object
Java
// Java Program to demonstrate ThreadInfo Class // Importing required librariesimport java.lang.management.ManagementFactory;import java.lang.management.ThreadInfo; // Maij classpublic class GFG {     // main driver method    public static void main(String[] args)    {        // Try block to check for exceptions        try {             // Creating a new thread by            // creating an object of Thread class            Thread thread = new Thread();             // running the thread using run() method            thread.run();             // Getting thread id using getId() method            long id = thread.getId();             // Creating a new ThreadInfo object            // using that id            ThreadInfo info                = ManagementFactory.getThreadMXBean()                      .getThreadInfo(id);             // Print and display message on the console            System.out.println(                "ThreadInfo object created successfully");        }         // Catch block to handle the exceptions        catch (Exception e) {             // print the line number where exception occurs            e.printStackTrace();        }    }}
Output:
ThreadInfo object created successfully
Example 2: Common-cleaner thread using ThreadInfo class
Java
// Java Program to demonstrate ThreadInfo Class // Importing required librariesimport java.lang.management.ManagementFactory;import java.lang.management.ThreadInfo; // Main classpublic class GFG {     // Main driver method    public static void main(String[] args)    {        // try block to check for exceptions        try {             long id = 10;             // Creating a new ThreadInfo object            // using this id            ThreadInfo info                = ManagementFactory.getThreadMXBean()                      .getThreadInfo(id);            // Printing information about the thread             // 1. Printing thread id            System.out.println("Thread ID: "                               + info.getThreadId());            // 2. Printing Thread Name            System.out.println("Thread Name: "                               + info.getThreadName());            // 3. Printing thread State            System.out.println("Thread State: "                               + info.getThreadState());            // 4. Printing thread waited count            System.out.println("Waited count: "                               + info.getWaitedCount());            // 5. Printing thread waited time            System.out.println("Waited time: "                               + info.getWaitedTime());            // 6. Printing how many times this thread had            // been blocked            System.out.println("Times blocked: "                               + info.getBlockedCount());            // 7. Printing Blocked duration            System.out.println("Blocked duration: "                               + info.getBlockedTime());            // 8. Printing Locked Monitors            System.out.println("Locked Monitors: "                               + info.getLockedMonitors());            // 9. Printing Locked Owner's ID            System.out.println("Locked Owner's ID: "                               + info.getLockOwnerId());            // 10. Printing Locked Owner's Name            System.out.println("Locked Owner's Name: "                               + info.getLockOwnerName());        }         // Catch block to handle the exceptions        catch (Exception e) {             // Print the line number where exception occured            e.printStackTrace();        }    }}
Output:
Thread ID: 10
Thread Name: Common-Cleaner
Thread State: TIMED_WAITING
Waited count: 1
Waited time: -1
Times blocked: 0
Blocked duration: -1
Locked Monitors: [Ljava.lang.management.MonitorInfo;@15aeb7ab
Locked Owner's ID: -1
Locked Owner's Name: null
kapoorsagar226
Java-lang package
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Introduction to Java
Comparator Interface in Java with Examples
PriorityQueue in Java
How to remove an element from ArrayList in Java?