當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java java.lang.management.ThreadInfo用法及代碼示例


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


相關用法


注:本文由純淨天空篩選整理自abhinavjain194大神的英文原創作品 java.lang.management.ThreadInfo Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。