FileDocCategorySizeDatePackage
ThreadInfo.javaAPI DocJava SE 6 API31019Tue Jun 10 00:25:38 BST 2008java.lang.management

ThreadInfo

public class ThreadInfo extends Object
Thread information. ThreadInfo contains the information about a thread including:

General thread information

  • Thread ID.
  • Name of the 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 {@link Object#wait Object.wait} method, or
    • parking due to a {@link java.util.concurrent.locks.LockSupport#park LockSupport.park} call.
  • 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.

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 {@link ThreadMXBean#setThreadContentionMonitoringEnabled thread contention monitoring} was enabled. Some Java virtual machine implementation may not support this. The {@link ThreadMXBean#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

ThreadInfo is mapped to a {@link CompositeData CompositeData} with attributes as specified in the {@link #from from} method.
see
ThreadMXBean#getThreadInfo
see
ThreadMXBean#dumpAllThreads
author
Mandy Chung
version
1.25, 05/05/06
since
1.5

Fields Summary
private String
threadName
private long
threadId
private long
blockedTime
private long
blockedCount
private long
waitedTime
private long
waitedCount
private LockInfo
lock
private String
lockName
private long
lockOwnerId
private String
lockOwnerName
private boolean
inNative
private boolean
suspended
private Thread$State
threadState
private StackTraceElement[]
stackTrace
private MonitorInfo[]
lockedMonitors
private LockInfo[]
lockedSynchronizers
private static MonitorInfo[]
EMPTY_MONITORS
private static LockInfo[]
EMPTY_SYNCS
private static final int
MAX_FRAMES
private static final StackTraceElement[]
NO_STACK_TRACE
Constructors Summary
private ThreadInfo(Thread t, int state, Object lockObj, Thread lockOwner, long blockedCount, long blockedTime, long waitedCount, long waitedTime, StackTraceElement[] stackTrace)
Constructor of ThreadInfo created by the JVM

param
t Thread
param
state Thread state
param
lockObj Object on which the thread is blocked
param
lockOwner the thread holding the lock
param
blockedCount Number of times blocked to enter a lock
param
blockedTime Approx time blocked to enter a lock
param
waitedCount Number of times waited on a lock
param
waitedTime Approx time waited on a lock
param
stackTrace Thread stack trace


                                                                                                                             
             
                          
                          
                         
        initialize(t, state, lockObj, lockOwner, 
                   blockedCount, blockedTime,
                   waitedCount, waitedTime, stackTrace, 
                   EMPTY_MONITORS, EMPTY_SYNCS);
    
private ThreadInfo(Thread t, int state, Object lockObj, Thread lockOwner, long blockedCount, long blockedTime, long waitedCount, long waitedTime, StackTraceElement[] stackTrace, Object[] monitors, int[] stackDepths, Object[] synchronizers)
Constructor of ThreadInfo created by the JVM for {@link ThreadMXBean#getThreadInfo(long[],boolean,boolean)} and {@link ThreadMXBean#dumpAllThreads}

param
t Thread
param
state Thread state
param
lockObj Object on which the thread is blocked
param
lockOwner the thread holding the lock
param
blockedCount Number of times blocked to enter a lock
param
blockedTime Approx time blocked to enter a lock
param
waitedCount Number of times waited on a lock
param
waitedTime Approx time waited on a lock
param
stackTrace Thread stack trace
param
lockedMonitors List of locked monitors
param
lockedSynchronizers List of locked synchronizers

        int numMonitors = (monitors == null ? 0 : monitors.length); 
        MonitorInfo[] lockedMonitors;
        if (numMonitors == 0) {
            lockedMonitors = EMPTY_MONITORS;
        } else {
            lockedMonitors = new MonitorInfo[numMonitors];
            for (int i = 0; i < numMonitors; i++) {
                Object lock = monitors[i];
                String className = lock.getClass().getName();
                int identityHashCode = System.identityHashCode(lock);
                int depth = stackDepths[i];
                StackTraceElement ste = (depth >= 0 ? stackTrace[depth]
                                                    : null);
                lockedMonitors[i] = new MonitorInfo(className,
                                                    identityHashCode,
                                                    depth,
                                                    ste);
            }
        } 

        int numSyncs = (synchronizers == null ? 0 : synchronizers.length); 
        LockInfo[] lockedSynchronizers;
        if (numSyncs == 0) {
            lockedSynchronizers = EMPTY_SYNCS;
        } else {
            lockedSynchronizers = new LockInfo[numSyncs];
            for (int i = 0; i < numSyncs; i++) {
                Object lock = synchronizers[i];
                String className = lock.getClass().getName();
                int identityHashCode = System.identityHashCode(lock);
                lockedSynchronizers[i] = new LockInfo(className,
                                                      identityHashCode);
            }
        }

        initialize(t, state, lockObj, lockOwner,
                   blockedCount, blockedTime,
                   waitedCount, waitedTime, stackTrace, 
                   lockedMonitors, lockedSynchronizers);
    
private ThreadInfo(CompositeData cd)

        ThreadInfoCompositeData ticd = ThreadInfoCompositeData.getInstance(cd);

        threadId = ticd.threadId(); 
        threadName = ticd.threadName(); 
        blockedTime = ticd.blockedTime();
        blockedCount = ticd.blockedCount();
        waitedTime = ticd.waitedTime();
        waitedCount = ticd.waitedCount();
        lockName = ticd.lockName();
        lockOwnerId = ticd.lockOwnerId();
        lockOwnerName = ticd.lockOwnerName();
        threadState = ticd.threadState();
        suspended = ticd.suspended();
        inNative = ticd.inNative();
        stackTrace = ticd.stackTrace();

        // 6.0 attributes
        if (ticd.isCurrentVersion()) {
            lock = ticd.lockInfo();
            lockedMonitors = ticd.lockedMonitors();
            lockedSynchronizers = ticd.lockedSynchronizers();
        } else {
            // lockInfo is a new attribute added in 1.6 ThreadInfo
            // If cd is a 5.0 version, construct the LockInfo object
            //  from the lockName value.
            if (lockName != null) { 
                String result[] = lockName.split("@");
                if (result.length == 2) {
                    int identityHashCode = Integer.parseInt(result[1], 16); 
                    lock = new LockInfo(result[0], identityHashCode);
                } else {
                    assert result.length == 2;
                    lock = null;
                }
            } else {
                lock = null;
            }
            lockedMonitors = EMPTY_MONITORS;
            lockedSynchronizers = EMPTY_SYNCS;
        }
    
Methods Summary
public static java.lang.management.ThreadInfofrom(javax.management.openmbean.CompositeData cd)
Returns a ThreadInfo object represented by the given CompositeData. The given CompositeData must contain the following attributes unless otherwise specified below:
Attribute Name Type
threadId java.lang.Long
threadName java.lang.String
threadState java.lang.String
suspended java.lang.Boolean
inNative java.lang.Boolean
blockedCount java.lang.Long
blockedTime java.lang.Long
waitedCount java.lang.Long
waitedTime java.lang.Long
lockInfo javax.management.openmbean.CompositeData - the mapped type for {@link LockInfo} as specified in the type mapping rules of {@linkplain javax.management.MXBean MXBeans}.

If cd does not contain this attribute, the LockInfo object will be constructed from the value of the lockName attribute.

lockName java.lang.String
lockOwnerId java.lang.Long
lockOwnerName java.lang.String
stackTrace javax.management.openmbean.CompositeData[]

Each element is a CompositeData representing StackTraceElement containing the following attributes:

Attribute Name Type
className java.lang.String
methodName java.lang.String
fileName java.lang.String
lineNumber java.lang.Integer
nativeMethod java.lang.Boolean
lockedMonitors javax.management.openmbean.CompositeData[] whose element type is the mapped type for {@link MonitorInfo} as specified in the {@link MonitorInfo#from Monitor.from} method.

If cd does not contain this attribute, this attribute will be set to an empty array.

lockedSynchronizers javax.management.openmbean.CompositeData[] whose element type is the mapped type for {@link LockInfo} as specified in the type mapping rules of {@linkplain javax.management.MXBean MXBeans}.

If cd does not contain this attribute, this attribute will be set to an empty array.

param
cd CompositeData representing a ThreadInfo
throws
IllegalArgumentException if cd does not represent a ThreadInfo with the attributes described above.
return
a ThreadInfo object represented by cd if cd is not null; null otherwise.


                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
         
        if (cd == null) {
            return null;
        }

        if (cd instanceof ThreadInfoCompositeData) {
            return ((ThreadInfoCompositeData) cd).getThreadInfo();
        } else {
            return new ThreadInfo(cd);
        }
    
public longgetBlockedCount()
Returns the total number of times that the thread associated with this ThreadInfo blocked to enter or reenter a monitor. I.e. the number of times a thread has been in the {@link java.lang.Thread.State#BLOCKED BLOCKED} state.

return
the total number of times that the thread entered the BLOCKED state.

        return blockedCount;
    
public longgetBlockedTime()
Returns the approximate accumulated elapsed time (in milliseconds) that the thread associated with this ThreadInfo has blocked to enter or reenter a monitor since thread contention monitoring is enabled. I.e. the total accumulated time the thread has been in the {@link java.lang.Thread.State#BLOCKED BLOCKED} state since thread contention monitoring was last enabled. This method returns -1 if 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.

return
the approximate accumulated elapsed time in milliseconds that a thread entered the BLOCKED state; -1 if thread contention monitoring is disabled.
throws
java.lang.UnsupportedOperationException if the Java virtual machine does not support this operation.
see
ThreadMXBean#isThreadContentionMonitoringSupported
see
ThreadMXBean#setThreadContentionMonitoringEnabled

        return blockedTime;
    
public java.lang.management.LockInfogetLockInfo()
Returns the LockInfo of an object for which the thread associated with this ThreadInfo is 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 the {@link java.lang.Thread.State#BLOCKED BLOCKED} state waiting to enter the synchronized statement or method.

  • an object monitor to be notified by another thread.
    The thread is in the {@link java.lang.Thread.State#WAITING WAITING} or {@link java.lang.Thread.State#TIMED_WAITING TIMED_WAITING} state due to a call to the {@link Object#wait Object.wait} method.

  • a synchronization object responsible for the thread parking.
    The thread is in the {@link java.lang.Thread.State#WAITING WAITING} or {@link java.lang.Thread.State#TIMED_WAITING TIMED_WAITING} state due to a call to the {@link java.util.concurrent.locks.LockSupport#park(Object) LockSupport.park} method. The synchronization object is the object returned from {@link java.util.concurrent.locks.LockSupport#getBlocker LockSupport.getBlocker} method. Typically it is an ownable synchronizer or a {@link java.util.concurrent.locks.Condition Condition}.

This method returns null if the thread is not in any of the above conditions.

return
LockInfo of an object for which the thread is blocked waiting if any; null otherwise.
since
1.6

        return lock;
    
public java.lang.StringgetLockName()
Returns the {@link LockInfo#toString string representation} of an object for which the thread associated with this ThreadInfo is blocked waiting. This method is equivalent to calling:
getLockInfo().toString()

This method will return null if this thread is not blocked waiting for any object or if the object is not owned by any thread.

return
the string representation of the object on which the thread is blocked if any; null otherwise.
see
#getLockInfo

        return lockName;
    
public longgetLockOwnerId()
Returns the ID of the thread which owns the object for which the thread associated with this ThreadInfo is blocked waiting. This method will return -1 if this thread is not blocked waiting for any object or if the object is not owned by any thread.

return
the thread ID of the owner thread of the object this thread is blocked on; -1 if this thread is not blocked or if the object lis not owned by any thread.
see
#getLockInfo

        return lockOwnerId;
    
public java.lang.StringgetLockOwnerName()
Returns the name of the thread which owns the object for which the thread associated with this ThreadInfo is blocked waiting. This method will return null if this thread is not blocked waiting for any object or if the object is not owned by any thread.

return
the name of the thread that owns the object this thread is blocked on; null if this thread is not blocked or if the object is not owned by any thread.
see
#getLockInfo

        return lockOwnerName;
    
public java.lang.management.MonitorInfo[]getLockedMonitors()
Returns an array of {@link MonitorInfo} objects, each of which represents an object monitor currently locked by the thread associated with this ThreadInfo. 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.

return
an array of MonitorInfo objects representing the object monitors locked by the thread.
since
1.6

        return lockedMonitors;
    
public java.lang.management.LockInfo[]getLockedSynchronizers()
Returns an array of {@link LockInfo} objects, each of which represents an ownable synchronizer currently locked by the thread associated with this ThreadInfo. 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.

return
an array of LockInfo objects representing the ownable synchronizers locked by the thread.
since
1.6

       // represents an <a href="LockInfo.html#OwnableSynchronizer">
        return lockedSynchronizers;
    
public java.lang.StackTraceElement[]getStackTrace()
Returns the stack trace of the thread associated with this ThreadInfo. 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 ThreadInfo is permitted to return a zero-length array from this method.

return
an array of StackTraceElement objects of the thread.

        return stackTrace;
    
public longgetThreadId()
Returns the ID of the thread associated with this ThreadInfo.

return
the ID of the associated thread.

        return threadId;
    
public java.lang.StringgetThreadName()
Returns the name of the thread associated with this ThreadInfo.

return
the name of the associated thread.

        return threadName;
    
public java.lang.Thread$StategetThreadState()
Returns the state of the thread associated with this ThreadInfo.

return
Thread.State of the associated thread.

         return threadState;
    
public longgetWaitedCount()
Returns the total number of times that the thread associated with this ThreadInfo waited for notification. I.e. the number of times that a thread has been in the {@link java.lang.Thread.State#WAITING WAITING} or {@link java.lang.Thread.State#TIMED_WAITING TIMED_WAITING} state.

return
the total number of times that the thread was in the WAITING or TIMED_WAITING state.

        return waitedCount;
    
public longgetWaitedTime()
Returns the approximate accumulated elapsed time (in milliseconds) that the thread associated with this ThreadInfo has waited for notification since thread contention monitoring is enabled. I.e. the total accumulated time the thread has been in the {@link java.lang.Thread.State#WAITING WAITING} or {@link java.lang.Thread.State#TIMED_WAITING TIMED_WAITING} state since thread contention monitoring is enabled. This method returns -1 if 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.

return
the approximate accumulated elapsed time in milliseconds that a thread has been in the WAITING or TIMED_WAITING state; -1 if thread contention monitoring is disabled.
throws
java.lang.UnsupportedOperationException if the Java virtual machine does not support this operation.
see
ThreadMXBean#isThreadContentionMonitoringSupported
see
ThreadMXBean#setThreadContentionMonitoringEnabled

        return waitedTime;
    
private voidinitialize(java.lang.Thread t, int state, java.lang.Object lockObj, java.lang.Thread lockOwner, long blockedCount, long blockedTime, long waitedCount, long waitedTime, java.lang.StackTraceElement[] stackTrace, java.lang.management.MonitorInfo[] lockedMonitors, java.lang.management.LockInfo[] lockedSynchronizers)
Initialize ThreadInfo object

param
t Thread
param
state Thread state
param
lockObj Object on which the thread is blocked
param
lockOwner the thread holding the lock
param
blockedCount Number of times blocked to enter a lock
param
blockedTime Approx time blocked to enter a lock
param
waitedCount Number of times waited on a lock
param
waitedTime Approx time waited on a lock
param
stackTrace Thread stack trace
param
lockedMonitors List of locked monitors
param
lockedSynchronizers List of locked synchronizers

        this.threadId = t.getId();
        this.threadName = t.getName();
        this.threadState = 
            sun.management.ManagementFactory.toThreadState(state);
        this.suspended =
            sun.management.ManagementFactory.isThreadSuspended(state);
        this.inNative =
            sun.management.ManagementFactory.isThreadRunningNative(state);
        this.blockedCount = blockedCount;
        this.blockedTime = blockedTime;
        this.waitedCount = waitedCount;
        this.waitedTime = waitedTime;

        if (lockObj == null) {
            this.lock = null;
            this.lockName = null;
        } else {
            this.lock = new LockInfo(lockObj);
            this.lockName = 
                lock.getClassName() + '@" +
                    Integer.toHexString(lock.getIdentityHashCode());
        }
        if (lockOwner == null) {
            this.lockOwnerId = -1; 
            this.lockOwnerName = null;
        } else {
            this.lockOwnerId = lockOwner.getId();
            this.lockOwnerName = lockOwner.getName();
        }
        if (stackTrace == null) {
            this.stackTrace = NO_STACK_TRACE;
        } else {
            this.stackTrace = stackTrace;
        }
        this.lockedMonitors = lockedMonitors;
        this.lockedSynchronizers = lockedSynchronizers;
    
public booleanisInNative()
Tests if the thread associated with this ThreadInfo is 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.

return
true if the thread is executing native code; false otherwise.

         return inNative;
    
public booleanisSuspended()
Tests if the thread associated with this ThreadInfo is suspended. This method returns true if {@link Thread#suspend} has been called.

return
true if the thread is suspended; false otherwise.

         return suspended;
    
public java.lang.StringtoString()
Returns a string representation of this thread info. The format of this string depends on the implementation. The returned string will typically include the {@linkplain #getThreadName thread name}, the {@linkplain #getThreadId thread ID}, its {@linkplain #getThreadState state}, and a {@linkplain #getStackTrace stack trace} if any.

return
a string representation of this thread info.

        StringBuilder sb = new StringBuilder("\"" + getThreadName() + "\"" +
                                             " Id=" + getThreadId() + " " +
                                             getThreadState());
        if (getLockName() != null) {
            sb.append(" on " + getLockName());
        }
        if (getLockOwnerName() != null) {
            sb.append(" owned by \"" + getLockOwnerName() +
                      "\" Id=" + getLockOwnerId());
        }
        if (isSuspended()) {
            sb.append(" (suspended)");
        }
        if (isInNative()) {
            sb.append(" (in native)");
        }
        sb.append('\n");
        int i = 0;
        for (; i < stackTrace.length && i < MAX_FRAMES; i++) {
            StackTraceElement ste = stackTrace[i];
            sb.append("\tat " + ste.toString());
            sb.append('\n");
            if (i == 0 && getLockInfo() != null) {
                Thread.State ts = getThreadState();
                switch (ts) {
                    case BLOCKED: 
                        sb.append("\t-  blocked on " + getLockInfo());
                        sb.append('\n");
                        break;
                    case WAITING:
                        sb.append("\t-  waiting on " + getLockInfo());
                        sb.append('\n");
                        break;
                    case TIMED_WAITING:
                        sb.append("\t-  waiting on " + getLockInfo());
                        sb.append('\n");
                        break;
                    default:
                }
            }

            for (MonitorInfo mi : lockedMonitors) {
                if (mi.getLockedStackDepth() == i) {
                    sb.append("\t-  locked " + mi);
                    sb.append('\n");
                }
            }
       }
       if (i < stackTrace.length) {
           sb.append("\t...");
           sb.append('\n");
       }
 
       LockInfo[] locks = getLockedSynchronizers();
       if (locks.length > 0) {
           sb.append("\n\tNumber of locked synchronizers = " + locks.length);
           sb.append('\n");
           for (LockInfo li : locks) {
               sb.append("\t- " + li);
               sb.append('\n");
           }
       }
       sb.append('\n");
       return sb.toString();