java.lang.management.ThreadInfo 类包含获取有关线程的信息的方法。这些信息包括:
- 线程ID
- 线程名称
- 线程的状态
- 线程的堆栈跟踪
- 线程被阻塞的对象
- 被线程阻塞的对象监视器列表
- 被线程阻塞的可拥有同步器列表
- 线程被阻塞的次数
- 线程被阻塞的时间
用法:类声明
public class ThreadInfo
extends Object
该类的方法如下:
方法 | 说明 |
---|---|
ThreadInfo 来自(CompositeData 数据) | 此方法用于将此复合数据表示为 ThreadInfo 对象。 |
getBlockedCount() | 此方法用于了解与此 ThreadInfo 对象关联的线程被阻止进入监视器或重新进入监视器的次数。 |
getBlockedTime() | 此方法用于了解与此 ThreadInfo 对象关联的线程在进入或重新进入监视器时被阻止了多少毫秒。 |
getLockedMonitors() | 该方法用于获取‘的列表MonitorInfo‘ 对象,当前由与此 ThreadInfo 对象关联的线程锁定。 |
getLockedSynchronizers() | 该方法用于获取‘的列表可拥有的‘ 同步器,当前由与此 ThreadInfo 对象关联的线程锁定。 |
getLockInfo() | 此方法用于获取与此 ThreadInfo 对象关联的线程被阻塞等待的对象的信息。它返回一个‘LockInfo' 代表信息的对象。 |
getLockName() | 此方法用于获取与此 ThreadInfo 对象关联的线程被阻塞等待的对象的名称。 |
getLockOwnerId() | 此方法用于获取拥有阻塞该线程的对象的线程的 ID。 |
getLockOwnerName() | 此方法用于获取拥有阻塞该线程的对象的线程的名称。 |
getStackTrace() | 该方法用于获取线程的堆栈跟踪。 |
getThreadId() | 该方法用于获取线程的ID。 |
getThreadName() | 该方法用于获取线程的名称。 |
getThreadState() | 该方法用于获取线程的状态。 |
getWaitedCount() | 此方法用于了解与此 ThreadInfo 对象关联的线程已等待通知的次数。 |
getWaitedTime() | 此方法用于了解与此 ThreadInfo 对象关联的线程已等待通知多少毫秒。 |
isInNative() | 该方法用于确定ThreadInfo对象是否通过java本机接口执行本机代码。 |
isSuspended() | 该方法用于确定与ThreadInfo对象关联的线程是否被挂起。 |
toString() | 此方法用于获取给定 ThreadInfo 对象的字符串表示形式。 |
执行:
示例 1:创建一个新的ThreadInfo对象
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();
}
}
}
输出:
ThreadInfo object created successfully
示例 2:Common-cleaner 线程使用 ThreadInfo 类
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();
}
}
}
输出:
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
相关用法
- Java java.lang.management.ManagementPermission用法及代码示例
- Java java.lang.reflect.AccessibleObject.getAnnotation()用法及代码示例
- Java java.lang.reflect.AccessibleObject.getAnnotations()用法及代码示例
- Java java.lang.reflect.AccessibleObject.getDeclaredAnnotations()用法及代码示例
- Java java.lang.reflect.AccessibleObject.isAccessible()用法及代码示例
- Java java.lang.reflect.AccessibleObject.setAccessible()用法及代码示例
- Java java.lang.reflect.Array.get()用法及代码示例
- Java java.lang.reflect.Array.getBoolean()用法及代码示例
- Java java.lang.reflect.Array.getByte()用法及代码示例
- Java java.lang.reflect.Array.getChar()用法及代码示例
- Java java.lang.reflect.Array.getDouble()用法及代码示例
- Java java.lang.reflect.Array.getFloat()用法及代码示例
- Java java.lang.reflect.Array.getInt()用法及代码示例
- Java java.lang.reflect.Array.getLength()用法及代码示例
- Java java.lang.reflect.Array.getLong()用法及代码示例
- Java java.lang.reflect.Array.getShort()用法及代码示例
- Java java.lang.reflect.Array.newInstance()用法及代码示例
- Java java.lang.reflect.Array.set()用法及代码示例
- Java java.lang.reflect.Array.setBoolean()用法及代码示例
- Java java.lang.reflect.Array.setByte()用法及代码示例
- Java java.lang.reflect.Array.setChar()用法及代码示例
- Java java.lang.reflect.Array.setDouble()用法及代码示例
- Java java.lang.reflect.Array.setFloat()用法及代码示例
- Java java.lang.reflect.Array.setInt()用法及代码示例
- Java java.lang.reflect.Array.setLong()用法及代码示例
注:本文由纯净天空筛选整理自abhinavjain194大神的英文原创作品 java.lang.management.ThreadInfo Class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。