java.lang.management.ThreadInfo Class in Java
Last Updated :
20 Jul, 2022
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 libraries
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
// Main class
public 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 libraries
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
// Main class
public 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
Similar Reads
Java.lang.ThreadLocal Class in Java This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. Basically, it is another way to achieve thread safety apart from writing im
4 min read
Java.lang.Runtime class in Java In Java, the Runtime class is used to interact with Every Java application that has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime() method. Methods of Java
6 min read
Java.lang.Void Class in Java Java.lang.Void class is a placeholder that holds a reference to a class object if it represents a void keyword. It is an uninstantiable placeholder. Well, uninstantiable means that this class has a private constructor and no other constructor that we can access from outside. Methods of lang.void cla
1 min read
Java.lang.Class class in Java | Set 1 Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It
15+ min read
Java.lang package in Java Java.lang package in JavaProvides classes that are fundamental to the design of the Java programming language. The most important classes are Object, which is the root of the class hierarchy, and Class, instances of which represent classes at run time. Following are the Important Classes in Java.lan
3 min read