| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
ThreadMXBean tm = ManagementFactory.getThreadMXBean();
Based on 135 examples
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 {@link ManagementFactory#getThreadMXBean} method or from the {@link ManagementFactory#getPlatformMBeanServer platform MBeanServer} method.
The ObjectName for uniquely identifying the MXBean for the thread system within an MBeanServer is:
{@link ManagementFactory#THREAD_MXBEAN_NAME 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 {@link #isThreadCpuTimeSupported} method can be used to determine if a Java virtual machine supports measuring of the CPU time for any thread. The {@link #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 {@link #isThreadCpuTimeEnabled} and {@link #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.
The {@link #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 {@link #setThreadContentionMonitoringEnabled} method can be used to enable thread contention monitoring.
The ThreadMXBean interface provides the {@link #findMonitorDeadlockedThreads} and {@link #findDeadlockedThreads} methods to find deadlocks in the running application.
Method Summary | |
---|---|
ThreadInfo[] |
dumpAllThreads(boolean lockedMonitors, boolean lockedSynchronizers) Returns the thread info for all live threads with stack trace and synchronization information. |
long[] |
Finds cycles of threads that are in deadlock waiting to acquire object monitors or ownable synchronizers. |
long[] |
Finds cycles of threads that are in deadlock waiting to acquire object monitors. |
long[] |
Returns all live thread IDs. |
long |
Returns the total CPU time for the current thread in nanoseconds. |
long |
Returns the CPU time that the current thread has executed in user mode in nanoseconds. |
int |
Returns the current number of live daemon threads. |
int |
Returns the peak live thread count since the Java virtual machine started or peak was reset. |
int |
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 id, int maxDepth) Returns a thread info for a thread of the specified id, with stack trace of a specified number of stack trace elements. |
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, boolean lockedMonitors, boolean lockedSynchronizers) Returns the thread info for each thread whose ID is in the input array ids, with stack trace and synchronization information. |
ThreadInfo[] |
getThreadInfo(long[] ids, int maxDepth) Returns the thread info for each thread whose ID is in the input array ids, with stack trace of a specified number of stack trace elements. |
long |
getThreadUserTime(long id) Returns the CPU time that a thread of the specified ID has executed in user mode in nanoseconds. |
long |
Returns the total number of threads created and also started since the Java virtual machine started. |
boolean |
Tests if the Java virtual machine supports CPU time measurement for the current thread. |
boolean |
Tests if the Java virtual machine supports monitoring of object monitor usage. |
boolean |
Tests if the Java virtual machine supports monitoring of ownable synchronizer usage. |
boolean |
Tests if thread contention monitoring is enabled. |
boolean |
Tests if the Java virtual machine supports thread contention monitoring. |
boolean |
Tests if thread CPU time measurement is enabled. |
boolean |
Tests if the Java virtual machine implementation supports CPU time measurement for any thread. |
void |
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 |
---|
public ThreadInfo[] dumpAllThreads(boolean lockedMonitors, boolean lockedSynchronizers)
This method returns an array of {@link ThreadInfo} objects as specified in the {@link #getThreadInfo(long[], boolean, boolean)} method.
lockedMonitors
- if true, dump all locked monitors.lockedSynchronizers
- if true, dump all locked
ownable synchronizers.public long[] findDeadlockedThreads()
This method is designed for troubleshooting use, but not for synchronization control. It might be an expensive operation.
public 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 {@link #findDeadlockedThreads findDeadlockedThreads} method should be used.
public long[] getAllThreadIds()
public long getCurrentThreadCpuTime()
This is a convenient method for local management use and is equivalent to calling:
{@link #getThreadCpuTime getThreadCpuTime}(Thread.currentThread().getId());
public long getCurrentThreadUserTime()
This is a convenient method for local management use and is equivalent to calling:
{@link #getThreadUserTime getThreadUserTime}(Thread.currentThread().getId());
public int getDaemonThreadCount()
public int getPeakThreadCount()
public int getThreadCount()
public 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.
id
- the thread ID of a threadpublic ThreadInfo getThreadInfo(long id)
{@link #getThreadInfo(long, int) getThreadInfo(id, 0);}
This method returns a ThreadInfo object representing the thread information for the thread of the specified ID. The stack trace, locked monitors, and locked synchronizers in the returned ThreadInfo object will be empty. 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 the
{@link ThreadInfo#from ThreadInfo.from} method.
id
- the thread ID of the thread. Must be positive.public 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 the
{@link ThreadInfo#from ThreadInfo.from} method.
id
- the thread ID of the thread. Must be positive.maxDepth
- the maximum number of entries in the stack trace
to be dumped. Integer.MAX_VALUE could be used to request
the entire stack to be dumped.public ThreadInfo[] getThreadInfo(long[] ids)
{@link #getThreadInfo(long[], int) getThreadInfo}(ids, 0);
This method returns an array of the ThreadInfo objects. The stack trace, locked monitors, and locked synchronizers in each ThreadInfo object will be empty. 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 the
{@link ThreadInfo#from ThreadInfo.from} method.
ids
- an array of thread IDs.public ThreadInfo[] getThreadInfo(long[] ids, boolean lockedMonitors, boolean lockedSynchronizers)
This method obtains a snapshot of the thread information for each thread including:
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.
If a thread does not lock any object monitor or lockedMonitors is false, the returned ThreadInfo object will have an empty MonitorInfo array. Similarly, if a thread does not lock any synchronizer or lockedSynchronizers is false, the returned ThreadInfo object will have an empty LockInfo array.
When both lockedMonitors and lockedSynchronizers parameters are false, it is equivalent to calling:
{@link #getThreadInfo(long[], int) getThreadInfo(ids, Integer.MAX_VALUE)}
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 is
CompositeData with attributes as specified in the
{@link ThreadInfo#from ThreadInfo.from} method.
ids
- an array of thread IDs.lockedMonitors
- if true, retrieves all locked monitors.lockedSynchronizers
- if true, retrieves all locked
ownable synchronizers.public 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 the
{@link ThreadInfo#from ThreadInfo.from} method.
ids
- an array of thread IDsmaxDepth
- the maximum number of entries in the stack trace
to be dumped. Integer.MAX_VALUE could be used to request
the entire stack to be dumped.public 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.
id
- the thread ID of a threadpublic long getTotalStartedThreadCount()
public boolean isCurrentThreadCpuTimeSupported()
public boolean isObjectMonitorUsageSupported()
public boolean isSynchronizerUsageSupported()
public boolean isThreadContentionMonitoringEnabled()
public boolean isThreadContentionMonitoringSupported()
public boolean isThreadCpuTimeEnabled()
public boolean isThreadCpuTimeSupported()
public void resetPeakThreadCount()
public void setThreadContentionMonitoringEnabled(boolean enable)
enable
- true to enable;
false to disable.public void setThreadCpuTimeEnabled(boolean enable)
enable
- true to enable;
false to disable.
| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |