- java.lang.Object
-
- java.lang.management.ThreadInfo
-
public class ThreadInfo extends Object
Thread information.ThreadInfocontains the information about a thread including:General thread information
- Thread ID.
- Name of the thread.
- Whether a thread is a daemon thread
Execution information
- Thread state.
- The object upon which the thread is blocked due to:
- waiting to enter a synchronization block/method, or
- waiting to be notified in a
Object.waitmethod, or - parking due to a
LockSupport.parkcall.
- The ID of the thread that owns the object that the thread is blocked.
- Stack trace of the thread.
- List of object monitors locked by the thread.
- List of ownable synchronizers locked by the thread.
- Thread priority
Synchronization Statistics
- The number of times that the thread has blocked for synchronization or waited for notification.
- The accumulated elapsed time that the thread has blocked
for synchronization or waited for notification
since
thread contention monitoringwas enabled. Some Java virtual machine implementation may not support this. TheThreadMXBean.isThreadContentionMonitoringSupported()method can be used to determine if a Java virtual machine supports this.
This thread information class is designed for use in monitoring of the system, not for synchronization control.
MXBean Mapping
ThreadInfois mapped to aCompositeDatawith attributes as specified in thefrommethod.- Since:
- 1.5
- See Also:
ThreadMXBean.getThreadInfo(long),ThreadMXBean.dumpAllThreads(boolean, boolean)
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ThreadInfofrom(CompositeData cd)Returns aThreadInfoobject represented by the givenCompositeData.longgetBlockedCount()Returns the total number of times that the thread associated with thisThreadInfoblocked to enter or reenter a monitor.longgetBlockedTime()Returns the approximate accumulated elapsed time (in milliseconds) that the thread associated with thisThreadInfohas blocked to enter or reenter a monitor since thread contention monitoring is enabled.MonitorInfo[]getLockedMonitors()Returns an array ofMonitorInfoobjects, each of which represents an object monitor currently locked by the thread associated with thisThreadInfo.LockInfo[]getLockedSynchronizers()Returns an array ofLockInfoobjects, each of which represents an ownable synchronizer currently locked by the thread associated with thisThreadInfo.LockInfogetLockInfo()Returns theLockInfoof an object for which the thread associated with thisThreadInfois blocked waiting.StringgetLockName()Returns thestring representationof an object for which the thread associated with thisThreadInfois blocked waiting.longgetLockOwnerId()Returns the ID of the thread which owns the object for which the thread associated with thisThreadInfois blocked waiting.StringgetLockOwnerName()Returns the name of the thread which owns the object for which the thread associated with thisThreadInfois blocked waiting.intgetPriority()Returns the thread priority of the thread associated with thisThreadInfo.StackTraceElement[]getStackTrace()Returns the stack trace of the thread associated with thisThreadInfo.longgetThreadId()Returns the ID of the thread associated with thisThreadInfo.StringgetThreadName()Returns the name of the thread associated with thisThreadInfo.Thread.StategetThreadState()Returns the state of the thread associated with thisThreadInfo.longgetWaitedCount()Returns the total number of times that the thread associated with thisThreadInfowaited for notification.longgetWaitedTime()Returns the approximate accumulated elapsed time (in milliseconds) that the thread associated with thisThreadInfohas waited for notification since thread contention monitoring is enabled.booleanisDaemon()Tests if the thread associated with thisThreadInfois a daemon thread.booleanisInNative()Tests if the thread associated with thisThreadInfois executing native code via the Java Native Interface (JNI).booleanisSuspended()Tests if the thread associated with thisThreadInfois suspended.StringtoString()Returns a string representation of this thread info.
-
-
-
Method Detail
-
getThreadId
public long getThreadId()
Returns the ID of the thread associated with thisThreadInfo.- Returns:
- the ID of the associated thread.
-
getThreadName
public String getThreadName()
Returns the name of the thread associated with thisThreadInfo.- Returns:
- the name of the associated thread.
-
getThreadState
public Thread.State getThreadState()
Returns the state of the thread associated with thisThreadInfo.- Returns:
Thread.Stateof the associated thread.
-
getBlockedTime
public long getBlockedTime()
Returns the approximate accumulated elapsed time (in milliseconds) that the thread associated with thisThreadInfohas blocked to enter or reenter a monitor since thread contention monitoring is enabled. I.e. the total accumulated time the thread has been in theBLOCKEDstate since thread contention monitoring was last enabled. This method returns-1if thread contention monitoring is disabled.The Java virtual machine may measure the time with a high resolution timer. This statistic is reset when the thread contention monitoring is reenabled.
- Returns:
- the approximate accumulated elapsed time in milliseconds
that a thread entered the
BLOCKEDstate;-1if thread contention monitoring is disabled. - Throws:
UnsupportedOperationException- if the Java virtual machine does not support this operation.- See Also:
ThreadMXBean.isThreadContentionMonitoringSupported(),ThreadMXBean.setThreadContentionMonitoringEnabled(boolean)
-
getBlockedCount
public long getBlockedCount()
Returns the total number of times that the thread associated with thisThreadInfoblocked to enter or reenter a monitor. I.e. the number of times a thread has been in theBLOCKEDstate.- Returns:
- the total number of times that the thread
entered the
BLOCKEDstate.
-
getWaitedTime
public long getWaitedTime()
Returns the approximate accumulated elapsed time (in milliseconds) that the thread associated with thisThreadInfohas waited for notification since thread contention monitoring is enabled. I.e. the total accumulated time the thread has been in theWAITINGorTIMED_WAITINGstate since thread contention monitoring is enabled. This method returns-1if thread contention monitoring is disabled.The Java virtual machine may measure the time with a high resolution timer. This statistic is reset when the thread contention monitoring is reenabled.
- Returns:
- the approximate accumulated elapsed time in milliseconds
that a thread has been in the
WAITINGorTIMED_WAITINGstate;-1if thread contention monitoring is disabled. - Throws:
UnsupportedOperationException- if the Java virtual machine does not support this operation.- See Also:
ThreadMXBean.isThreadContentionMonitoringSupported(),ThreadMXBean.setThreadContentionMonitoringEnabled(boolean)
-
getWaitedCount
public long getWaitedCount()
Returns the total number of times that the thread associated with thisThreadInfowaited for notification. I.e. the number of times that a thread has been in theWAITINGorTIMED_WAITINGstate.- Returns:
- the total number of times that the thread
was in the
WAITINGorTIMED_WAITINGstate.
-
getLockInfo
public LockInfo getLockInfo()
Returns theLockInfoof an object for which the thread associated with thisThreadInfois blocked waiting. A thread can be blocked waiting for one of the following:- an object monitor to be acquired for entering or reentering
a synchronization block/method.
The thread is in theBLOCKEDstate waiting to enter thesynchronizedstatement or method. - an object monitor to be notified by another thread.
The thread is in theWAITINGorTIMED_WAITINGstate due to a call to theObject.waitmethod. - a synchronization object responsible for the thread parking.
The thread is in theWAITINGorTIMED_WAITINGstate due to a call to theLockSupport.parkmethod. The synchronization object is the object returned fromLockSupport.getBlockermethod. Typically it is an ownable synchronizer or aCondition.
This method returns
nullif the thread is not in any of the above conditions.- Returns:
LockInfoof an object for which the thread is blocked waiting if any;nullotherwise.- Since:
- 1.6
- an object monitor to be acquired for entering or reentering
a synchronization block/method.
-
getLockName
public String getLockName()
Returns thestring representationof an object for which the thread associated with thisThreadInfois blocked waiting. This method is equivalent to calling:getLockInfo().toString()
This method will return
nullif this thread is not blocked waiting for any object or if the object is not owned by any thread.- Returns:
- the string representation of the object on which
the thread is blocked if any;
nullotherwise. - See Also:
getLockInfo()
-
getLockOwnerId
public long getLockOwnerId()
Returns the ID of the thread which owns the object for which the thread associated with thisThreadInfois blocked waiting. This method will return-1if this thread is not blocked waiting for any object or if the object is not owned by any thread.- Returns:
- the thread ID of the owner thread of the object
this thread is blocked on;
-1if this thread is not blocked or if the object is not owned by any thread. - See Also:
getLockInfo()
-
getLockOwnerName
public String getLockOwnerName()
Returns the name of the thread which owns the object for which the thread associated with thisThreadInfois blocked waiting. This method will returnnullif this thread is not blocked waiting for any object or if the object is not owned by any thread.- Returns:
- the name of the thread that owns the object
this thread is blocked on;
nullif this thread is not blocked or if the object is not owned by any thread. - See Also:
getLockInfo()
-
getStackTrace
public StackTraceElement[] getStackTrace()
Returns the stack trace of the thread associated with thisThreadInfo. If no stack trace was requested for this thread info, this method will return a zero-length array. If the returned array is of non-zero length then the first element of the array represents the top of the stack, which is the most recent method invocation in the sequence. The last element of the array represents the bottom of the stack, which is the least recent method invocation in the sequence.Some Java virtual machines may, under some circumstances, omit one or more stack frames from the stack trace. In the extreme case, a virtual machine that has no stack trace information concerning the thread associated with this
ThreadInfois permitted to return a zero-length array from this method.- Returns:
- an array of
StackTraceElementobjects of the thread.
-
isSuspended
public boolean isSuspended()
Tests if the thread associated with thisThreadInfois suspended. This method returnstrueifThread.suspend()has been called.- Returns:
trueif the thread is suspended;falseotherwise.
-
isInNative
public boolean isInNative()
Tests if the thread associated with thisThreadInfois executing native code via the Java Native Interface (JNI). The JNI native code does not include the virtual machine support code or the compiled native code generated by the virtual machine.- Returns:
trueif the thread is executing native code;falseotherwise.
-
isDaemon
public boolean isDaemon()
Tests if the thread associated with thisThreadInfois a daemon thread.- Returns:
trueif the thread is a daemon thread,falseotherwise.- Since:
- 9
- See Also:
Thread.isDaemon()
-
getPriority
public int getPriority()
Returns the thread priority of the thread associated with thisThreadInfo.- Returns:
- The priority of the thread associated with this
ThreadInfo. - Since:
- 9
-
toString
public String toString()
Returns a string representation of this thread info. The format of this string depends on the implementation. The returned string will typically include the thread name, the thread ID, its state, and a stack trace if any.
-
from
public static ThreadInfo from(CompositeData cd)
Returns aThreadInfoobject represented by the givenCompositeData. ACompositeDatarepresenting aThreadInfoof version N must contain all of the attributes defined in version ≤ N unless specified otherwise. The same rule applies the composite type of the givenCompositeDataand transitively to attributes whose type or component type isCompositeType.A
CompositeDatarepresentingThreadInfoof version N contains"stackTrace"attribute and"lockedMonitors"attribute representing an array ofStackTraceElementand an array ofMonitorInforespectively and their types are of version N. The"lockedStackFrame"attribute inMonitorInfo's composite type must representStackTraceElementof the same version N. Otherwise, this method will throwIllegalArgumentException.
AAttribute Name Type Since threadId java.lang.Long5 threadName java.lang.String5 threadState java.lang.String5 suspended java.lang.Boolean5 inNative java.lang.Boolean5 blockedCount java.lang.Long5 blockedTime java.lang.Long5 waitedCount java.lang.Long5 waitedTime java.lang.Long5 lockName java.lang.String5 lockOwnerId java.lang.Long5 lockOwnerName java.lang.String5 stackTrace javax.management.openmbean.CompositeData[], each element is aCompositeDatarepresentingStackTraceElementas specified below.5 lockInfo javax.management.openmbean.CompositeData- the mapped type forLockInfoas specified in theLockInfo.from(javax.management.openmbean.CompositeData)method.If the given
CompositeDatadoes not contain this attribute, theLockInfoobject will be constructed from the value of thelockNameattribute.6 lockedMonitors javax.management.openmbean.CompositeData[]whose element type is the mapped type forMonitorInfoas specified in theMonitorInfo.frommethod.If the given
CompositeDatadoes not contain this attribute, this attribute will be set to an empty array.6 lockedSynchronizers javax.management.openmbean.CompositeData[]whose element type is the mapped type forLockInfoas specified in theLockInfo.from(javax.management.openmbean.CompositeData)method.If the given
CompositeDatadoes not contain this attribute, this attribute will be set to an empty array.6 daemon java.lang.BooleanIf the given
CompositeDatadoes not contain this attribute, this attribute will be set tofalse.9 priority java.lang.IntegerIf the given
CompositeDatadoes not contain this attribute, This attribute will be set toThread.NORM_PRIORITY.9 CompositeDatarepresentingStackTraceElementof version N must contain all of the attributes defined in version ≤ N unless specified otherwise.Attribute Name Type Since classLoaderName java.lang.String9 moduleName java.lang.String9 moduleVersion java.lang.String9 className java.lang.String5 methodName java.lang.String5 fileName java.lang.String5 lineNumber java.lang.Integer5 nativeMethod java.lang.Boolean5 - Parameters:
cd-CompositeDatarepresenting aThreadInfo- Returns:
- a
ThreadInfoobject represented bycdifcdis notnull;nullotherwise. - Throws:
IllegalArgumentException- if the givencdand its composite type does not contain all of the attributes defined for aThreadInfoof a specific runtime version.
-
getLockedMonitors
public MonitorInfo[] getLockedMonitors()
Returns an array ofMonitorInfoobjects, each of which represents an object monitor currently locked by the thread associated with thisThreadInfo. If no locked monitor was requested for this thread info or no monitor is locked by the thread, this method will return a zero-length array.- Returns:
- an array of
MonitorInfoobjects representing the object monitors locked by the thread. - Since:
- 1.6
-
getLockedSynchronizers
public LockInfo[] getLockedSynchronizers()
Returns an array ofLockInfoobjects, each of which represents an ownable synchronizer currently locked by the thread associated with thisThreadInfo. If no locked synchronizer was requested for this thread info or no synchronizer is locked by the thread, this method will return a zero-length array.- Returns:
- an array of
LockInfoobjects representing the ownable synchronizers locked by the thread. - Since:
- 1.6
-
-