is new.
public interface ThreadMXBean
The management interface for the thread system of the Java virtual machine.
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
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 thread. The isCurrentThreadCpuTimeSupported() method can be used to determine if a Java virtual machine supports measuring of the CPU time for the current thread. A Java virtual machine implementation that supports CPU time measurement for any thread will also support that for the current 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(boolean) 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.
| Method Summary | |
|---|---|
long[]
|
findDeadlockedThreads
()
Finds cycles of threads that are in deadlock waiting to acquire object monitors or
ownable synchronizers
|
| long[] |
findMonitorDeadlockedThreads
() Finds cycles of threads that are in deadlock waiting to acquire object monitors. |
Map
<
ThreadInfo
,
MonitorInfo
|
getAllLockedMonitors
()
Returns a map of object monitors currently locked by all live threads.
|
Map
<
ThreadInfo
,
LockInfo
|
getAllLockedSynchronizers
()
Returns a map of
ownable synchronizers
|
| long[] |
getAllThreadIds
() Returns all live thread IDs. |
| long |
getCurrentThreadCpuTime
() Returns the total CPU time for the current thread in nanoseconds. |
| long |
getCurrentThreadUserTime
() Returns the CPU time that the current thread has executed in user mode in nanoseconds. |
| int |
getDaemonThreadCount
() Returns the current number of live daemon threads. |
| int |
getPeakThreadCount
() Returns the peak live thread count since the Java virtual machine started or peak was reset. |
| int |
getThreadCount
() Returns the current number of live threads including both daemon and non-daemon threads. |
| long |
getThreadCpuTime
(long id) Returns the total CPU time for a thread of the specified ID in nanoseconds. |
| ThreadInfo |
getThreadInfo
(long id) Returns the thread info for a thread of the specified id with no stack trace. |
| ThreadInfo [] |
getThreadInfo
(long[] ids) Returns the thread info for each thread whose ID is in the input array ids with no stack trace. |
| ThreadInfo [] |
getThreadInfo
(long[] ids, int maxDepth) Returns the thread info for each thread whose ID is in the input array ids . |
| ThreadInfo |
getThreadInfo
(long id, int maxDepth) Returns a thread info for a thread of the specified id . |
| long |
getThreadUserTime
(long id) Returns the CPU time that a thread of the specified ID has executed in user mode in nanoseconds. |
| long |
getTotalStartedThreadCount
() Returns the total number of threads created and also started since the Java virtual machine started. |
| boolean |
isCurrentThreadCpuTimeSupported
() Tests if the Java virtual machine supports CPU time measurement for the current thread. |
boolean
|
isObjectMonitorUsageSupported
()
Tests if the Java virtual machine supports monitoring of object monitor usage.
|
boolean
|
isSynchronizerUsageSupported
()
Tests if the Java virtual machine supports monitoring of
ownable synchronizer
|
| boolean |
isThreadContentionMonitoringEnabled
() Tests if thread contention monitoring is enabled. |
| boolean |
isThreadContentionMonitoringSupported
() Tests if the Java virtual machine supports thread contention monitoring. |
| boolean |
isThreadCpuTimeEnabled
() Tests if thread CPU time measurement is enabled. |
| boolean |
isThreadCpuTimeSupported
() Tests if the Java virtual machine implementation supports CPU time measurement for any thread. |
| void |
resetPeakThreadCount
() Resets the peak thread count to the current number of live threads. |
| void |
setThreadContentionMonitoringEnabled
(boolean enable) Enables or disables thread contention monitoring. |
| void |
setThreadCpuTimeEnabled
(boolean enable) Enables or disables thread CPU time measurement. |
| Method Detail |
|---|
int getThreadCount()
int getPeakThreadCount()
long getTotalStartedThreadCount()
int getDaemonThreadCount()
long[] getAllThreadIds()
ThreadInfo getThreadInfo(long id)
getThreadInfo(id, 0);
This method returns a ThreadInfo object representing the thread information for the thread of the specified ID. The stack trace in the returned ThreadInfo object will be an empty array of StackTraceElement . If a thread of the given ID is not alive or does not exist, this method will return null . A thread is alive if it has been started and has not yet died.
MBeanServer access
:
The mapped type of
ThreadInfo
is
CompositeData
with attributes as specified in
ThreadInfo
.
ThreadInfo[] getThreadInfo(long[] ids)
getThreadInfo(ids, 0);
This method returns an array of the ThreadInfo objects. The stack trace in each ThreadInfo object will be an empty array of StackTraceElement . If a thread of a given ID is not alive or does not exist, the corresponding element in the returned array will contain null . A thread is alive if it has been started and has not yet died.
MBeanServer access
:
The mapped type of
ThreadInfo
is
CompositeData
with attributes as specified in
ThreadInfo
.
ThreadInfo getThreadInfo(long id,
int maxDepth)
When the Java virtual machine has no stack trace information about a thread or maxDepth == 0 , the stack trace in the ThreadInfo object will be an empty array of StackTraceElement .
If a thread of the given ID is not alive or does not exist, this method will return null . A thread is alive if it has been started and has not yet died.
MBeanServer access
:
The mapped type of
ThreadInfo
is
CompositeData
with attributes as specified in
ThreadInfo
.
ThreadInfo[] getThreadInfo(long[] ids,
int maxDepth)
When the Java virtual machine has no stack trace information about a thread or maxDepth == 0 , the stack trace in the ThreadInfo object will be an empty array of StackTraceElement .
This method returns an array of the ThreadInfo objects, each is the thread information about the thread with the same index as in the ids array. If a thread of the given ID is not alive or does not exist, null will be set in the corresponding element in the returned array. A thread is alive if it has been started and has not yet died.
MBeanServer access
:
The mapped type of
ThreadInfo
is
CompositeData
with attributes as specified in
ThreadInfo
.
boolean isThreadContentionMonitoringSupported()
boolean isThreadContentionMonitoringEnabled()
void setThreadContentionMonitoringEnabled(boolean enable)
long getCurrentThreadCpuTime()
precision
This is a convenient method for local management use and is equivalent to calling:
getThreadCpuTime(Thread.currentThread().getId());
long getCurrentThreadUserTime()
precision
This is a convenient method for local management use and is equivalent to calling:
getThreadUserTime(Thread.currentThread().getId());
long getThreadCpuTime(long id)
If the thread of the specified ID is not alive or does not exist, this method returns -1 . If CPU time measurement is disabled, this method returns -1 . A thread is alive if it has been started and has not yet died.
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.
long getThreadUserTime(long id)
If the thread of the specified ID is not alive or does not exist, this method returns -1 . If CPU time measurement is disabled, this method returns -1 . A thread is alive if it has been started and has not yet died.
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.
boolean isThreadCpuTimeSupported()
boolean isCurrentThreadCpuTimeSupported()
boolean isThreadCpuTimeEnabled()
void setThreadCpuTimeEnabled(boolean enable)
long[] findMonitorDeadlockedThreads()
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
findDeadlockedThreads
method should be used.
See Also:
findDeadlockedThreads()
void resetPeakThreadCount()
findDeadlockedThreads
long[]
findDeadlockedThreads
()
Finds cycles of threads that are in deadlock waiting to acquire object monitors or
ownable synchronizers
. 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 thread in the cycle.
This method is designed for troubleshooting use, but not for synchronization control. It might be an expensive operation.
Returns:
an array of IDs of the threads that are deadlocked waiting for object monitors or ownable synchronizers, if any;
null
otherwise.
Throws:
SecurityException
- if a security manager exists and the caller does not have ManagementPermission("monitor").
UnsupportedOperationException
- if the Java virtual machine does not support monitoriing of ownable synchronizer usage.
Since:
1.6
See Also:
isSynchronizerUsageSupported()
,
findMonitorDeadlockedThreads()
getAllLockedMonitors
Map
<
ThreadInfo
,
MonitorInfo
[]>
getAllLockedMonitors
()
Returns a map of object monitors currently locked by all live threads. The returned map contains one entry for each thread. The map keys are of type
ThreadInfo
which contains the thread information of a thread and its entire stack trace. Each map value is an array of
MonitorInfo
that represents the object monitors locked by the corresponding thread. If the thread has no object monitor locked, the map value will be a zero-length array.
This method is designed for troubleshooting use, but not for synchronization control. It might be an expensive operation.
MBeanServer access
:
The mapped type of
ThreadInfo
and
MonitorInfo
is
CompositeData
with attributes as specified in
ThreadInfo
and
MonitorInfo
respectively.
Returns:
a
Map
from
ThreadInfo
to an array of
MonitorInfo
that represents the object monitors locked by the corresponding thread.
Throws:
SecurityException
- if a security manager exists and the caller does not have ManagementPermission("monitor").
UnsupportedOperationException
- if the Java virtual machine does not support monitoriing of object monitor usage.
Since:
1.6
See Also:
isObjectMonitorUsageSupported()
isObjectMonitorUsageSupported
boolean
isObjectMonitorUsageSupported
()
Tests if the Java virtual machine supports monitoring of object monitor usage.
Returns:
true
if the Java virtual machine supports monitoring of object monitor usage;
false
otherwise.
Since:
1.6
See Also:
getAllLockedMonitors()
getAllLockedSynchronizers
Map
<
ThreadInfo
,
LockInfo
[]>
getAllLockedSynchronizers
()
Returns a map of
ownable synchronizers
currently locked by all live threads. The returned map contains one entry for each thread. The map keys are of type
ThreadInfo
which contains the thread information of a thread and its entire stack trace. Each map value is an array of
LockInfo
that represents the synchronizers locked by the corresponding thread. If the thread has no synchronizer locked, the map value will be a zero-length array.
This method is designed for troubleshooting use, but not for synchronization control. It might be an expensive operation.
MBeanServer access
:
The mapped type of
ThreadInfo
and
LockInfo
is
CompositeData
with attributes as specified in
ThreadInfo
and
LockInfo
respectively.
Returns:
a
Map
from
ThreadInfo
to an array of
LockInfo
that represents the synchronizers locked by the corresponding thread.
Throws:
SecurityException
- if a security manager exists and the caller does not have ManagementPermission("monitor").
UnsupportedOperationException
- if the Java virtual machine does not support monitoring of ownable synchronizer usage.
Since:
1.6
See Also:
isSynchronizerUsageSupported()
isSynchronizerUsageSupported
boolean
isSynchronizerUsageSupported
()
Tests if the Java virtual machine supports monitoring of
ownable synchronizer
usage.
Returns:
true
if the Java virtual machine supports monitoring of ownable synchronizer usage;
false
otherwise.
Since:
1.6
See Also:
getAllLockedSynchronizers()