ThreadMXBean supports monitoring and management of
platform threads in the Java
virtual machine. Platform threads are typically mapped to kernel
threads scheduled by the operating system. ThreadMXBean
does not support monitoring or management of virtual threads.
A Java virtual machine has a single instance of the implementation
class of this interface. This instance implementing this interface is
an MXBean
that can be obtained by calling
the ManagementFactory#getThreadMXBean method or
from the platform MBeanServer method.
The ObjectName for uniquely identifying the MXBean for
the thread system within an MBeanServer is:
java.lang:type=Threading
It can be obtained by calling the
PlatformManagedObject#getObjectName method.
threadId() method.
The thread ID is unique during its lifetime. When the thread terminates,
its thread ID may be reused.
Some methods in this interface take a thread ID or an array of thread IDs as the input parameter and return per-thread information.
The isThreadCpuTimeSupported method can be used to determine
if a Java virtual machine supports measuring of the CPU time for any
platform thread. The isCurrentThreadCpuTimeSupported() method
can be used to determine if a Java virtual machine supports measuring of
the CPU time with the getCurrentThreadCpuTime() and
getCurrentThreadUserTime() methods from a platform thread.
The CPU time provided by this interface has nanosecond precision but not necessarily nanosecond accuracy.
A Java virtual machine may disable CPU time measurement
by default.
The isThreadCpuTimeEnabled and setThreadCpuTimeEnabled
methods can be used to test if CPU time measurement is enabled
and to enable/disable this support respectively.
Enabling thread CPU measurement could be expensive in some
Java virtual machine implementations.
ThreadInfo object.
The isThreadContentionMonitoringSupported method can be used to
determine if a Java virtual machine supports thread contention monitoring.
The thread contention monitoring is disabled by default. The
setThreadContentionMonitoringEnabled method can be used to enable
thread contention monitoring.
getThreadInfo(long[], boolean, boolean) and
dumpAllThreads methods can be used to obtain the thread stack trace
and synchronization information including which
lock a thread is blocked to
acquire or waiting on and which locks the thread currently owns.
The ThreadMXBean interface provides the
findMonitorDeadlockedThreads and
findDeadlockedThreads methods to find deadlocks in
the running application.
ManagementFactory#getPlatformMXBeans(Class), JMX Specification., Ways to Access MXBeans
| Modifier and Type | Method and Description |
|---|---|
| public ThreadInfo[] | Returns: an array ofThreadInfo for all live platform threads.if lockedMonitors, boolean true, dump all locked monitors.if lockedSynchronizers)true, dump all locked
ownable synchronizers.Returns the thread info for all live platform threads with stack trace and synchronization information. |
| public default ThreadInfo[] | Returns: an array ofThreadInfo for all live platform threads.if lockedMonitors, boolean true, dump all locked monitors.if lockedSynchronizers, int true, dump all locked
ownable synchronizers.indicates the maximum number of
maxDepth)StackTraceElement to be retrieved from the stack trace.Returns the thread info for all live platform threads with stack trace of the specified maximum number of elements and synchronization information. |
| public long[] | Returns: an array of IDs of the platform threads that are deadlocked waiting for object monitors or ownable synchronizers, if any;null otherwise.Finds cycles of platform threads that are in deadlock waiting to acquire object monitors or ownable synchronizers. |
| public long[] | Returns: an array of IDs of the platform threads that are monitor deadlocked, if any;null otherwise.Finds cycles of platform threads that are in deadlock waiting to acquire object monitors. |
| public long[] | Returns: an array oflong, each is a thread ID.Returns the threadIDs of all live platform threads. |
| public long | Returns: the total CPU time for the current thread if the current thread is a platform thread and if CPU time measurement is enabled;-1 otherwise.Returns the total CPU time for the current thread in nanoseconds. |
| public long | Returns: the user-level CPU time for the current thread if the current thread is a platform thread and if CPU time measurement is enabled;-1 otherwise.Returns the CPU time that the current thread has executed in user mode in nanoseconds. |
| public int | Returns: the current number of live platform threads that are daemon threads.Returns the current number of live platform threads that are daemon threads. |
| public int | Returns: the peak live platform thread count.Returns the peak live platform thread count since the Java virtual machine started or peak was reset. |
| public int | Returns: the current number of live platform threads.Returns the current number of live platform threads including both daemon and non-daemon threads. |
| public long | Returns: the total CPU time for a thread of the specified ID if the thread of the specified ID is a platform thread, the thread is alive, and CPU time measurement is enabled;-1 otherwise.the thread ID of a thread id)Returns the total CPU time for a thread of the specified ID in nanoseconds. |
| public ThreadInfo | Returns: aThreadInfo object for the thread of the given ID
with no stack trace, no locked monitor and no synchronizer info;
null if the thread of the given ID is a virtual thread,
is not alive, or it does not exist.the thread ID of the thread. Must be positive. id)Returns the thread info for a thread of the specified
|
| public ThreadInfo[] | Returns: an array of theThreadInfo objects, each containing
information about a thread whose ID is in the corresponding
element of the input array of IDs
with no stack trace, no locked monitor and no synchronizer info.an array of thread IDs. ids)Returns the thread info for each thread
whose ID is in the input array |
| public ThreadInfo | Returns: aThreadInfo of the thread of the given ID
with no locked monitor and synchronizer info.
null if the thread of the given ID is a virtual thread, is
not alive or it does not exist.the thread ID of the thread. Must be positive. id, int the maximum number of entries in the stack trace
to be dumped. maxDepth)Integer.MAX_VALUE could be used to request
the entire stack to be dumped.Returns a thread info for a thread of the specified |
| public ThreadInfo[] | Returns: an array of theThreadInfo objects, each containing
information about a thread whose ID is in the corresponding
element of the input array of IDs with no locked monitor and
synchronizer info.an array of thread IDs ids, int the maximum number of entries in the stack trace
to be dumped. maxDepth)Integer.MAX_VALUE could be used to request
the entire stack to be dumped.Returns the thread info for each thread
whose ID is in the input array |
| public ThreadInfo[] | Returns: an array of theThreadInfo objects, each containing
information about a thread whose ID is in the corresponding
element of the input array of IDs.an array of thread IDs. ids, boolean if lockedMonitors, boolean true, retrieves all locked monitors.if lockedSynchronizers)true, retrieves all locked
ownable synchronizers.Returns the thread info for each thread
whose ID is in the input array |
| public default ThreadInfo[] | Returns: an array of theThreadInfo objects, each containing
information about a thread whose ID is in the corresponding
element of the input array of IDs.an array of thread IDs. ids, boolean if lockedMonitors, boolean true, retrieves all locked monitors.if lockedSynchronizers, int true, retrieves all locked
ownable synchronizers.indicates the maximum number of
maxDepth)StackTraceElement to be retrieved from the stack trace.Returns the thread info for each thread whose ID
is in the input array |
| public long | Returns: the user-level CPU time for a thread of the specified ID if the thread of the specified ID is a platform thread, the thread is alive, and CPU time measurement is enabled;-1 otherwise.the thread ID of a thread id)Returns the CPU time that a thread of the specified ID has executed in user mode in nanoseconds. |
| public long | Returns: the total number of platform threads started.Returns the total number of platform threads created and also started since the Java virtual machine started. |
| public boolean | Returns: true
if the Java virtual machine supports CPU time measurement
of the current platform thread;
false otherwise.Tests if the Java virtual machine supports CPU time measurement from
a platform thread with the |
| public boolean | Returns: true
if the Java virtual machine supports monitoring of
object monitor usage;
false otherwise.Tests if the Java virtual machine supports monitoring of object monitor usage. |
| public boolean | Returns: true
if the Java virtual machine supports monitoring of ownable
synchronizer usage;
false otherwise.Tests if the Java virtual machine supports monitoring of ownable synchronizer usage. |
| public boolean | Returns: true if thread contention monitoring is enabled;
false otherwise.Tests if thread contention monitoring is enabled. |
| public boolean | Returns: true
if the Java virtual machine supports thread contention monitoring;
false otherwise.Tests if the Java virtual machine supports thread contention monitoring. |
| public boolean | Returns: true if thread CPU time measurement is enabled;
false otherwise.Tests if thread CPU time measurement is enabled. |
| public boolean | Returns: true
if the Java virtual machine supports CPU time
measurement for any platform thread;
false otherwise.Tests if the Java virtual machine implementation supports CPU time measurement for any platform thread. |
| public void | |
| public void | setThreadContentionMonitoringEnabled(boolean
true to enable;
false to disable.Enables or disables thread contention monitoring. |
| public void | setThreadCpuTimeEnabled(boolean
true to enable;
false to disable.Enables or disables thread CPU time measurement. |
| dumpAllThreads | back to summary |
|---|---|
| public ThreadInfo[] dumpAllThreads(boolean lockedMonitors, boolean lockedSynchronizers) Returns the thread info for all live platform threads with stack trace and synchronization information. The thread IDs of virtual threads are not included. This method is equivalent to calling:
| |
| dumpAllThreads | back to summary |
|---|---|
| public default ThreadInfo[] dumpAllThreads(boolean lockedMonitors, boolean lockedSynchronizers, int maxDepth) Returns the thread info for all live platform threads
with stack trace of the specified maximum number of elements
and synchronization information.
if
This method returns an array of Implementation Specification The default implementation throws
| |
| findDeadlockedThreads | back to summary |
|---|---|
| public long[] findDeadlockedThreads() Finds cycles of platform threads that are in deadlock waiting to acquire object monitors or ownable synchronizers. Platform threads are deadlocked in a cycle waiting for a lock of these two types if each thread owns one lock while trying to acquire another lock already held by another platform thread in the cycle. Cycles that include virtual threads are not found by this method. This method is designed for troubleshooting use, but not for synchronization control. It might be an expensive operation.
| |
| findMonitorDeadlockedThreads | back to summary |
|---|---|
| public long[] findMonitorDeadlockedThreads() Finds cycles of platform threads that are in deadlock waiting to acquire
object monitors. That is, platform threads that are blocked waiting to
enter a synchronization block or waiting to reenter a synchronization block
after an More formally, a thread is monitor deadlocked if it is part of a cycle in the relation "is waiting for an object monitor owned by". In the simplest case, thread A is blocked waiting for a monitor owned by thread B, and thread B is blocked waiting for a monitor owned by thread A. This method is designed for troubleshooting use, but not for synchronization control. It might be an expensive operation.
This method finds deadlocks involving only object monitors.
To find deadlocks involving both object monitors and
ownable synchronizers,
the
| |
| getAllThreadIds | back to summary |
|---|---|
| public long[] getAllThreadIds() Returns the threadIDs of all live platform threads. The thread IDs of virtual threads are not included. Some threads included in the returned array may have been terminated when this method returns.
| |
| getCurrentThreadCpuTime | back to summary |
|---|---|
| public long getCurrentThreadCpuTime() Returns the total CPU time for the current thread in nanoseconds. The returned value is of nanoseconds precision but not necessarily nanoseconds accuracy. If the implementation distinguishes between user mode time and system mode time, the returned CPU time is the amount of time that the current thread has executed in user mode or system mode. This is a convenience method for local management use and is equivalent to calling:
| |
| getCurrentThreadUserTime | back to summary |
|---|---|
| public long getCurrentThreadUserTime() Returns the CPU time that the current thread has executed in user mode in nanoseconds. The returned value is of nanoseconds precision but not necessarily nanoseconds accuracy. This is a convenience method for local management use and is equivalent to calling:
| |
| getDaemonThreadCount | back to summary |
|---|---|
| public int getDaemonThreadCount() Returns the current number of live platform threads that are daemon threads. The count does not include virtual threads.
| |
| getPeakThreadCount | back to summary |
|---|---|
| public int getPeakThreadCount() Returns the peak live platform thread count since the Java virtual machine started or peak was reset. The count does not include virtual threads.
| |
| getThreadCount | back to summary |
|---|---|
| public int getThreadCount() Returns the current number of live platform threads including both daemon and non-daemon threads. The count does not include virtual threads.
| |
| getThreadCpuTime | back to summary |
|---|---|
| public long getThreadCpuTime(long id) Returns the total CPU time for a thread of the specified ID in nanoseconds. The returned value is of nanoseconds precision but not necessarily nanoseconds accuracy. If the implementation distinguishes between user mode time and system mode time, the returned CPU time is the amount of time that the thread has executed in user mode or system mode.
If the thread of the specified ID is a virtual thread, is not alive or
does not exist, this method returns If CPU time measurement is enabled after the thread has started, the Java virtual machine implementation may choose any time up to and including the time that the capability is enabled as the point where CPU time measurement starts.
| |
| getThreadInfo | back to summary |
|---|---|
| public ThreadInfo getThreadInfo(long id) Returns the thread info for a thread of the specified
This method returns a
MBeanServer access:
| |
| getThreadInfo | back to summary |
|---|---|
| public ThreadInfo[] getThreadInfo(long[] ids) Returns the thread info for each thread
whose ID is in the input array
This method returns an array of the
MBeanServer access:
| |
| getThreadInfo | back to summary |
|---|---|
| public ThreadInfo getThreadInfo(long id, int maxDepth) Returns a thread info for a thread of the specified
When the Java virtual machine has no stack trace information
about a thread or
If a thread of the given ID is a virtual thread, is not alive, or does
not exist, this method will return
MBeanServer access:
| |
| getThreadInfo | back to summary |
|---|---|
| public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) Returns the thread info for each thread
whose ID is in the input array
When the Java virtual machine has no stack trace information
about a thread or
This method returns an array of the
MBeanServer access:
| |
| getThreadInfo | back to summary |
|---|---|
| public ThreadInfo[] getThreadInfo(long[] ids, boolean lockedMonitors, boolean lockedSynchronizers) Returns the thread info for each thread
whose ID is in the input array
| |
| getThreadInfo | back to summary |
|---|---|
| public default ThreadInfo[] getThreadInfo(long[] ids, boolean lockedMonitors, boolean lockedSynchronizers, int maxDepth) Returns the thread info for each thread whose ID
is in the input array This method obtains a snapshot of the thread information for each thread including:
This method returns an array of the
If a thread does not lock any object monitor or
When both
This method is designed for troubleshooting use, but not for synchronization control. It might be an expensive operation.
MBeanServer access: Implementation Specification The default implementation throws
| |
| getThreadUserTime | back to summary |
|---|---|
| public long getThreadUserTime(long id) Returns the CPU time that a thread of the specified ID has executed in user mode in nanoseconds. The returned value is of nanoseconds precision but not necessarily nanoseconds accuracy.
If the thread of the specified ID is a virtual thread, is not alive, or
does not exist, this method returns If CPU time measurement is enabled after the thread has started, the Java virtual machine implementation may choose any time up to and including the time that the capability is enabled as the point where CPU time measurement starts.
| |
| getTotalStartedThreadCount | back to summary |
|---|---|
| public long getTotalStartedThreadCount() Returns the total number of platform threads created and also started since the Java virtual machine started. The count does not include virtual threads.
| |
| isCurrentThreadCpuTimeSupported | back to summary |
|---|---|
| public boolean isCurrentThreadCpuTimeSupported() Tests if the Java virtual machine supports CPU time measurement from
a platform thread with the
| |
| isObjectMonitorUsageSupported | back to summary |
|---|---|
| public boolean isObjectMonitorUsageSupported() Tests if the Java virtual machine supports monitoring of object monitor usage.
| |
| isSynchronizerUsageSupported | back to summary |
|---|---|
| public boolean isSynchronizerUsageSupported() Tests if the Java virtual machine supports monitoring of ownable synchronizer usage.
| |
| isThreadContentionMonitoringEnabled | back to summary |
|---|---|
| public boolean isThreadContentionMonitoringEnabled() Tests if thread contention monitoring is enabled.
| |
| isThreadContentionMonitoringSupported | back to summary |
|---|---|
| public boolean isThreadContentionMonitoringSupported() Tests if the Java virtual machine supports thread contention monitoring.
| |
| isThreadCpuTimeEnabled | back to summary |
|---|---|
| public boolean isThreadCpuTimeEnabled() Tests if thread CPU time measurement is enabled.
| |
| isThreadCpuTimeSupported | back to summary |
|---|---|
| public boolean isThreadCpuTimeSupported() Tests if the Java virtual machine implementation supports CPU time measurement for any platform thread. A Java virtual machine implementation that supports CPU time measurement for any platform thread will also support CPU time measurement for the current thread, when the current thread is a platform thread.
| |
| resetPeakThreadCount | back to summary |
|---|---|
| public void resetPeakThreadCount() Resets the peak thread count to the current number of live platform threads.
| |
| setThreadContentionMonitoringEnabled | back to summary |
|---|---|
| public void setThreadContentionMonitoringEnabled(boolean enable) Enables or disables thread contention monitoring. Thread contention monitoring is disabled by default.
| |
| setThreadCpuTimeEnabled | back to summary |
|---|---|
| public void setThreadCpuTimeEnabled(boolean enable) Enables or disables thread CPU time measurement. The default is platform dependent.
| |