java.lang.management.ThreadInfo Class in Java

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:
| Methods | Description |
|---|---|
| ThreadInfo from(CompositeData data) | This method is used to represent this composite data as a ThreadInfo object. |
| getBlockedCount() | This method is used to know how many times the thread associated with this ThreadInfo object had been blocked to enter a monitor or reenter a monitor. |
| getBlockedTime() | This method is used to know for many milliseconds the thread associated with this ThreadInfo object had been blocked to enter or reenter a monitor. |
| getLockedMonitors() | This method is used to get a list of ‘MonitorInfo‘ objects, which are currently locked by the thread associated with this ThreadInfo object. |
| getLockedSynchronizers() | This method is used to get a list of ‘ownable‘ synchronizers, which are currently locked by the thread associated with this ThreadInfo object. |
| getLockInfo() | This method is used to get information about the object for which the thread associated with this ThreadInfo object is blocked waiting. It returns a ‘LockInfo‘ object representing the information. |
| getLockName() | This method is used to get the name of the object for which the thread associated with this ThreadInfo object is blocked waiting. |
| getLockOwnerId() | This method is used to get the ID of the thread which owns the object that is blocking this thread. |
| getLockOwnerName() | This method is used to get the Name of the thread which owns the object that is blocking this thread. |
| getStackTrace() | This method is used to get the stack trace of a Thread. |
| getThreadId() | This method is used to get the ID of a Thread. |
| getThreadName() | This method is used to get the name of a Thread. |
| getThreadState() | This method is used to get the state of a Thread. |
| getWaitedCount() | This method is used to know how many times the thread associated with this ThreadInfo object has waited for notification. |
| getWaitedTime() | This method is used to know for many milliseconds the thread associated with this ThreadInfo object has waited for notification. |
| isInNative() | This method is used to determine whether this ThreadInfo object is executing the native code via the java native interface or not. |
| isSuspended() | This method is used to determine whether the Thread associated with this ThreadInfo object is suspended or not. |
| toString() | This method is used to get string representation of the given ThreadInfo object. |
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;// Main 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 occurred 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



