📜  Java的.lang.management.ThreadInfo类在Java中

📅  最后修改于: 2022-05-13 01:55:22.731000             🧑  作者: Mango

Java的.lang.management.ThreadInfo类在Java中

Java.lang.management.ThreadInfo 类包含获取线程信息的方法。这些信息包括:

  • 线程 ID
  • 线程名称
  • 线程状态
  • 线程的堆栈跟踪
  • 线程被阻塞的对象
  • 被线程阻塞的对象监视器列表
  • 线程阻塞的可拥有同步器列表
  • 线程被阻塞的次数
  • 线程被阻塞的时间

语法:类声明

public class ThreadInfo
extends Object

该类的方法如下:

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

执行:

示例 1:创建一个新的 ThreadInfo 对象



Java
// Java Program to demonstrate ThreadInfo Class
 
// Importing required libraries
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
 
// Maij 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();
        }
    }
}


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 occured
            e.printStackTrace();
        }
    }
}


输出:

ThreadInfo object created successfully

示例 2:使用 ThreadInfo 类的 Common-cleaner 线程

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 occured
            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