HHeLiBeXの日記 正道編

日々の記憶の記録とメモ‥

java.lang.management パッケージ - ThreadMXBean インタフェース

ManagementFactory クラスを介して取得できるMXBeanの一つが実装するインタフェース。

文字通りスレッドに関連する情報を取得するためのMXBean。
スレッドに関する情報はアプリケーションの実行とともに動的に変わっていくので、とりあえず今回はJava VM実行開始直後の情報を取得して表示するということだけをやってみる。また、ThreadMXBeanやそこから取得できるThreadInfoにはサポートされないかもしれない操作が数多くあるので注意が必要。
実際にやってみる。
今回用いたのは、次のJava VM

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.printf("%-23s = %s\n", "java.version", System.getProperty("java.version"));
        System.out.printf("%-23s = %s\n", "java.vendor", System.getProperty("java.vendor"));
        System.out.printf("%-23s = %s\n", "java.runtime.version", System.getProperty("java.runtime.version"));
        System.out.printf("%-23s = %s\n", "os.name", System.getProperty("os.name"));

        System.out.println("========");

        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        System.out.printf("%-32s: %s\n", "isThreadContentionMonitoringSupported", threadMXBean.isThreadContentionMonitoringSupported());
        if (threadMXBean.isThreadContentionMonitoringSupported()) {
            System.out.printf("%-32s: %s\n", "isThreadContentionMonitoringEnabled", threadMXBean.isThreadContentionMonitoringEnabled());

            threadMXBean.setThreadContentionMonitoringEnabled(true);
        }
        System.out.printf("%-32s: %s\n", "isThreadCpuTimeSupported", threadMXBean.isThreadCpuTimeSupported());
        if (threadMXBean.isThreadCpuTimeSupported()) {
            System.out.printf("%-32s: %s\n", "isThreadCpuTimeEnabled", threadMXBean.isThreadCpuTimeEnabled());

            threadMXBean.setThreadCpuTimeEnabled(true);
        }

        System.out.printf("%-32s:\n", "findMonitorDeadlockedThreads");
        {
            long[] monitorDeadlockedThreads = threadMXBean.findMonitorDeadlockedThreads();
            if (monitorDeadlockedThreads != null) {
                int i = 0;
                for (long id : monitorDeadlockedThreads) {
                    System.out.printf("    [%2d]: %20d\n", i, id);
    
                    ++i;
                }
            }
        }
        System.out.printf("%-32s:\n", "AllThreadIds");
        {
            long[] allThreadIds = threadMXBean.getAllThreadIds();
            int i = 0;
            for (long id : allThreadIds) {
                System.out.printf("    [%2d]: %20d\n", i, id);

                if (threadMXBean.isThreadCpuTimeSupported()) {
                    System.out.printf("        %-32s: %s\n", "ThreadCpuTime", threadMXBean.getThreadCpuTime(id));
                    System.out.printf("        %-32s: %s\n", "ThreadUserTime", threadMXBean.getThreadUserTime(id));
                }
                System.out.printf("        %-32s:\n", "ThreadInfo");
                {
                    ThreadInfo threadInfo = threadMXBean.getThreadInfo(id);
                    System.out.printf("        %-32s: %20d\n", "BlockedCount", threadInfo.getBlockedCount());
                    if (threadMXBean.isThreadContentionMonitoringSupported()) {
                        System.out.printf("        %-32s: %20d\n", "BlockedTime", threadInfo.getBlockedTime());
                    }
                    System.out.printf("        %-32s: %s\n", "LockName", threadInfo.getLockName());
                    System.out.printf("        %-32s: %20d\n", "LockOwnerId", threadInfo.getLockOwnerId());
                    System.out.printf("        %-32s: %s\n", "LockOwnerName", threadInfo.getLockOwnerName());
                    System.out.printf("        %-32s:\n", "StackTrace (omit the details)");
                    {
                        StackTraceElement[] stackTrace = threadInfo.getStackTrace();
                        System.out.printf("            %-32s: %d\n", "# of stack trace", stackTrace.length);
                    }
                    System.out.printf("        %-32s: %20d\n", "ThreadId", threadInfo.getThreadId());
                    System.out.printf("        %-32s: %s\n", "ThreadName", threadInfo.getThreadName());
                    System.out.printf("        %-32s: %s\n", "ThreadState", threadInfo.getThreadState());
                    System.out.printf("        %-32s: %20d\n", "WaitedCount", threadInfo.getWaitedCount());
                    if (threadMXBean.isThreadContentionMonitoringSupported()) {
                        System.out.printf("        %-32s: %20d\n", "WaitedTime", threadInfo.getWaitedTime());
                    }
                    System.out.printf("        %-32s: %s\n", "isInNative", threadInfo.isInNative());
                    System.out.printf("        %-32s: %s\n", "isSuspended", threadInfo.isSuspended());
                }

                ++i;
            }
        }
        System.out.printf("%-32s: %s\n", "isCurrentThreadCpuTimeSupported", threadMXBean.isCurrentThreadCpuTimeSupported());
        if (threadMXBean.isCurrentThreadCpuTimeSupported()) {
            System.out.printf("%-32s: %20d\n", "CurrentThreadCpuTime", threadMXBean.getCurrentThreadCpuTime());
            System.out.printf("%-32s: %20d\n", "CurrentThreadUserTime", threadMXBean.getCurrentThreadUserTime());
        }
        System.out.printf("%-32s: %10d\n", "DaemonThreadCount", threadMXBean.getDaemonThreadCount());
        System.out.printf("%-32s: %10d\n", "PeakThreadCount", threadMXBean.getPeakThreadCount());
        System.out.printf("%-32s: %10d\n", "ThreadCount", threadMXBean.getThreadCount());
        System.out.printf("%-32s: %20d\n", "TotalStartedThreadCount", threadMXBean.getTotalStartedThreadCount());

//        threadMXBean.resetPeakThreadCount();

//        threadMXBean.getThreadInfo(ids);
//        threadMXBean.getThreadInfo(id, maxDepth);
//        threadMXBean.getThreadInfo(ids, maxDepth);
    }

}

Sun Java 2 SDK 1.5.0_22

java.version            = 1.5.0_22
java.vendor             = Sun Microsystems Inc.
java.runtime.version    = 1.5.0_22-b03
os.name                 = Windows Vista
========
isThreadContentionMonitoringSupported: true
isThreadContentionMonitoringEnabled: false
isThreadCpuTimeSupported        : true
isThreadCpuTimeEnabled          : true
findMonitorDeadlockedThreads    :
AllThreadIds                    :
    [ 0]:                    4
        ThreadCpuTime                   : 0
        ThreadUserTime                  : 0
        ThreadInfo                      :
        BlockedCount                    :                    0
        BlockedTime                     :                    0
        LockName                        : null
        LockOwnerId                     :                   -1
        LockOwnerName                   : null
        StackTrace (omit the details)   :
            # of stack trace                : 0
        ThreadId                        :                    4
        ThreadName                      : Signal Dispatcher
        ThreadState                     : RUNNABLE
        WaitedCount                     :                    0
        WaitedTime                      :                    0
        isInNative                      : false
        isSuspended                     : false
    [ 1]:                    3
        ThreadCpuTime                   : 0
        ThreadUserTime                  : 0
        ThreadInfo                      :
        BlockedCount                    :                    0
        BlockedTime                     :                    0
        LockName                        : java.lang.ref.ReferenceQueue$Lock@60aeb0
        LockOwnerId                     :                   -1
        LockOwnerName                   : null
        StackTrace (omit the details)   :
            # of stack trace                : 0
        ThreadId                        :                    3
        ThreadName                      : Finalizer
        ThreadState                     : WAITING
        WaitedCount                     :                    1
        WaitedTime                      :                    0
        isInNative                      : false
        isSuspended                     : false
    [ 2]:                    2
        ThreadCpuTime                   : 0
        ThreadUserTime                  : 0
        ThreadInfo                      :
        BlockedCount                    :                    0
        BlockedTime                     :                    0
        LockName                        : java.lang.ref.Reference$Lock@16caf43
        LockOwnerId                     :                   -1
        LockOwnerName                   : null
        StackTrace (omit the details)   :
            # of stack trace                : 0
        ThreadId                        :                    2
        ThreadName                      : Reference Handler
        ThreadState                     : WAITING
        WaitedCount                     :                    1
        WaitedTime                      :                    0
        isInNative                      : false
        isSuspended                     : false
    [ 3]:                    1
        ThreadCpuTime                   : 234001500
        ThreadUserTime                  : 187201200
        ThreadInfo                      :
        BlockedCount                    :                    0
        BlockedTime                     :                    0
        LockName                        : null
        LockOwnerId                     :                   -1
        LockOwnerName                   : null
        StackTrace (omit the details)   :
            # of stack trace                : 0
        ThreadId                        :                    1
        ThreadName                      : main
        ThreadState                     : RUNNABLE
        WaitedCount                     :                    0
        WaitedTime                      :                    0
        isInNative                      : false
        isSuspended                     : false
isCurrentThreadCpuTimeSupported : true
CurrentThreadCpuTime            :            234001500
CurrentThreadUserTime           :            187201200
DaemonThreadCount               :          3
PeakThreadCount                 :          4
ThreadCount                     :          4
TotalStartedThreadCount         :                    4

IBM Java 2 SDK 1.5.0 SR10

java.version            = 1.5.0
java.vendor             = IBM Corporation
java.runtime.version    = pwi32dev-20090707 (SR10 )
os.name                 = Windows Vista
========
isThreadContentionMonitoringSupported: true
isThreadContentionMonitoringEnabled: false
isThreadCpuTimeSupported        : true
isThreadCpuTimeEnabled          : true
findMonitorDeadlockedThreads    :
AllThreadIds                    :
    [ 0]:                    1
        ThreadCpuTime                   : 530403400
        ThreadUserTime                  : 436802800
        ThreadInfo                      :
        BlockedCount                    :                    0
        BlockedTime                     :                    0
        LockName                        : null
        LockOwnerId                     :                   -1
        LockOwnerName                   : null
        StackTrace (omit the details)   :
            # of stack trace                : 0
        ThreadId                        :                    1
        ThreadName                      : main
        ThreadState                     : RUNNABLE
        WaitedCount                     :                    0
        WaitedTime                      :                    0
        isInNative                      : true
        isSuspended                     : false
    [ 1]:                    3
        ThreadCpuTime                   : 124800800
        ThreadUserTime                  : 124800800
        ThreadInfo                      :
        BlockedCount                    :                    0
        BlockedTime                     :                    0
        LockName                        : null
        LockOwnerId                     :                   -1
        LockOwnerName                   : null
        StackTrace (omit the details)   :
            # of stack trace                : 0
        ThreadId                        :                    3
        ThreadName                      : JIT Compilation Thread
        ThreadState                     : RUNNABLE
        WaitedCount                     :                    0
        WaitedTime                      :                    0
        isInNative                      : false
        isSuspended                     : false
    [ 2]:                    2
        ThreadCpuTime                   : 0
        ThreadUserTime                  : 0
        ThreadInfo                      :
        BlockedCount                    :                    0
        BlockedTime                     :                    0
        LockName                        : null
        LockOwnerId                     :                   -1
        LockOwnerName                   : null
        StackTrace (omit the details)   :
            # of stack trace                : 0
        ThreadId                        :                    2
        ThreadName                      : Signal Dispatcher
        ThreadState                     : RUNNABLE
        WaitedCount                     :                    0
        WaitedTime                      :                    0
        isInNative                      : true
        isSuspended                     : false
    [ 3]:                    4
        ThreadCpuTime                   : 0
        ThreadUserTime                  : 0
        ThreadInfo                      :
        BlockedCount                    :                    0
        BlockedTime                     :                    0
        LockName                        : null
        LockOwnerId                     :                   -1
        LockOwnerName                   : null
        StackTrace (omit the details)   :
            # of stack trace                : 0
        ThreadId                        :                    4
        ThreadName                      : Gc Slave Thread
        ThreadState                     : RUNNABLE
        WaitedCount                     :                    0
        WaitedTime                      :                    0
        isInNative                      : false
        isSuspended                     : false
isCurrentThreadCpuTimeSupported : true
CurrentThreadCpuTime            :            577203700
CurrentThreadUserTime           :            483603100
DaemonThreadCount               :          3
PeakThreadCount                 :          4
ThreadCount                     :          4
TotalStartedThreadCount         :                    4
  • とりあえず、サポートされない操作はなかったようで。
  • findMonitorDeadlockedThreads
    • まぁ、スレッドを起こしてないのだから、該当するものはないわな。
  • StackTrace

動的に変わっていく各々の項目については次回以降にいろいろ試すということで。