KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > lang > management > ThreadInfo


1 /*
2  * @(#)ThreadInfo.java 1.16 04/04/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.lang.management;
9
10 import javax.management.openmbean.CompositeData JavaDoc;
11 import sun.management.ThreadInfoCompositeData;
12
13 /**
14  * Thread information. <tt>ThreadInfo</tt> contains the information
15  * about a thread including:
16  * <h4>General thread information</h4>
17  * <ul>
18  * <li>Thread ID.</li>
19  * <li>Name of the thread.</li>
20  * </ul>
21  *
22  * <h4>Execution information</h4>
23  * <ul>
24  * <li>Thread state.</tt>
25  * <li>The object upon which the thread is blocked waiting to enter
26  * a synchronization block or waiting to be notified in
27  * a {@link Object#wait Object.wait} call.</li>
28  * <li>The ID of the thread that owns the object
29  * that the thread is blocked.</li>
30  * <li>Stack trace of the thread.</li>
31  * </ul>
32  *
33  * <h4>Synchronization statistics</h4>
34  * <ul>
35  * <li>The number of times that the thread has blocked for
36  * synchronization or waited for notification.</li>
37  * <li>The accumulated elapsed time that the thread has blocked
38  * for synchronization or waited for notification
39  * since thread contention monitoring
40  * was enabled. Some Java virtual machine implementation
41  * may not support this. The
42  * {@link ThreadMXBean#isThreadContentionMonitoringSupported()}
43  * method can be used to determine if a Java virtual machine
44  * supports this.</li>
45  * </ul>
46  *
47  * <p>This thread information class is designed for use in monitoring of
48  * the system, not for synchronization control.
49  *
50  * <h4>MXBean Mapping</h4>
51  * <tt>ThreadInfo</tt> is mapped to a {@link CompositeData CompositeData}
52  * with attributes as specified in
53  * the {@link #from from} method.
54  *
55  * @see ThreadMXBean#isThreadContentionMonitoringSupported
56  *
57  * @author Mandy Chung
58  * @version 1.16, 04/18/04
59  * @since 1.5
60  */

61
62 public class ThreadInfo {
63     private final String JavaDoc threadName;
64     private final long threadId;
65     private final long blockedTime;
66     private final long blockedCount;
67     private final long waitedTime;
68     private final long waitedCount;
69     private final String JavaDoc lockName;
70     private final long lockOwnerId;
71     private final String JavaDoc lockOwnerName;
72     private final boolean inNative;
73     private final boolean suspended;
74     private final Thread.State JavaDoc threadState;
75     private final StackTraceElement JavaDoc[] stackTrace;
76
77     /**
78      * Constructor of ThreadInfo created by the JVM
79      *
80      * @param t Thread
81      * @param state Thread state
82      * @param lockObj Object on which the thread is blocked
83      * to enter or waiting
84      * @param lockOwner the thread holding the lock
85      * @param blockedCount Number of times blocked to enter a lock
86      * @param blockedTime Approx time blocked to enter a lock
87      * @param waitedCount Number of times waited on a lock
88      * @param waitedTime Approx time waited on a lock
89      * @param stackTrace Thread stack trace
90      */

91     private ThreadInfo(Thread JavaDoc t, int state, Object JavaDoc lockObj, Thread JavaDoc lockOwner,
92                        long blockedCount, long blockedTime,
93                        long waitedCount, long waitedTime,
94                        StackTraceElement JavaDoc[] stackTrace) {
95         this.threadId = t.getId();
96         this.threadName = t.getName();
97         this.threadState =
98             sun.management.ManagementFactory.toThreadState(state);
99         this.suspended =
100             sun.management.ManagementFactory.isThreadSuspended(state);
101         this.inNative =
102             sun.management.ManagementFactory.isThreadRunningNative(state);
103         this.blockedCount = blockedCount;
104         this.blockedTime = blockedTime;
105         this.waitedCount = waitedCount;
106         this.waitedTime = waitedTime;
107
108         if (lockObj == null) {
109             this.lockName = null;
110         } else {
111             this.lockName =
112                 lockObj.getClass().getName() + '@' +
113                     Integer.toHexString(System.identityHashCode(lockObj));
114         }
115         if (lockOwner == null) {
116             this.lockOwnerId = -1;
117             this.lockOwnerName = null;
118         } else {;
119             this.lockOwnerId = lockOwner.getId();
120             this.lockOwnerName = lockOwner.getName();
121         }
122         this.stackTrace = stackTrace;
123     }
124
125     /*
126      * Constructs a <tt>ThreadInfo</tt> object from a
127      * {@link CompositeData CompositeData}.
128      */

129     private ThreadInfo(CompositeData JavaDoc cd) {
130         ThreadInfoCompositeData.validateCompositeData(cd);
131
132         threadId = ThreadInfoCompositeData.getThreadId(cd);
133         threadName = ThreadInfoCompositeData.getThreadName(cd);
134         blockedTime = ThreadInfoCompositeData.getBlockedTime(cd);
135         blockedCount = ThreadInfoCompositeData.getBlockedCount(cd);
136         waitedTime = ThreadInfoCompositeData.getWaitedTime(cd);
137         waitedCount = ThreadInfoCompositeData.getWaitedCount(cd);
138         lockName = ThreadInfoCompositeData.getLockName(cd);
139         lockOwnerId = ThreadInfoCompositeData.getLockOwnerId(cd);
140         lockOwnerName = ThreadInfoCompositeData.getLockOwnerName(cd);
141         threadState = ThreadInfoCompositeData.getThreadState(cd);
142         suspended = ThreadInfoCompositeData.isSuspended(cd);
143         inNative = ThreadInfoCompositeData.isInNative(cd);
144         stackTrace = ThreadInfoCompositeData.getStackTrace(cd);
145     }
146
147                       
148     /**
149      * Returns the ID of the thread associated with this <tt>ThreadInfo</tt>.
150      *
151      * @return the ID of the associated thread.
152      */

153     public long getThreadId() {
154         return threadId;
155     }
156
157     /**
158      * Returns the name of the thread associated with this <tt>ThreadInfo</tt>.
159      *
160      * @return the name of the associated thread.
161      */

162     public String JavaDoc getThreadName() {
163         return threadName;
164     }
165
166     /**
167      * Returns the state of the thread associated with this <tt>ThreadInfo</tt>.
168      *
169      * @return <tt>Thread.State</tt> of the associated thread.
170      */

171     public Thread.State JavaDoc getThreadState() {
172          return threadState;
173     }
174     
175     /**
176      * Returns the approximate accumulated elapsed time (in milliseconds)
177      * that the thread associated with this <tt>ThreadInfo</tt>
178      * has blocked to enter or reenter a monitor
179      * since thread contention monitoring is enabled.
180      * I.e. the total accumulated time the thread has been in the
181      * {@link java.lang.Thread.State#BLOCKED BLOCKED} state since thread
182      * contention monitoring was last enabled.
183      * This method returns <tt>-1</tt> if thread contention monitoring
184      * is disabled.
185      *
186      * <p>The Java virtual machine may measure the time with a high
187      * resolution timer. This statistic is reset when
188      * the thread contention monitoring is reenabled.
189      *
190      * @return the approximate accumulated elapsed time in milliseconds
191      * that a thread entered the <tt>BLOCKED</tt> state;
192      * <tt>-1</tt> if thread contention monitoring is disabled.
193      *
194      * @throws java.lang.UnsupportedOperationException if the Java
195      * virtual machine does not support this operation.
196      *
197      * @see ThreadMXBean#isThreadContentionMonitoringSupported
198      * @see ThreadMXBean#setThreadContentionMonitoringEnabled
199      */

200     public long getBlockedTime() {
201         return blockedTime;
202     }
203
204     /**
205      * Returns the total number of times that
206      * the thread associated with this <tt>ThreadInfo</tt>
207      * blocked to enter or reenter a monitor.
208      * I.e. the number of times a thread has been in the
209      * {@link java.lang.Thread.State#BLOCKED BLOCKED} state.
210      *
211      * @return the total number of times that the thread
212      * entered the <tt>BLOCKED</tt> state.
213      */

214     public long getBlockedCount() {
215         return blockedCount;
216     }
217
218     /**
219      * Returns the approximate accumulated elapsed time (in milliseconds)
220      * that the thread associated with this <tt>ThreadInfo</tt>
221      * has waited for notification
222      * since thread contention monitoring is enabled.
223      * I.e. the total accumulated time the thread has been in the
224      * {@link java.lang.Thread.State#WAITING WAITING}
225      * or {@link java.lang.Thread.State#TIMED_WAITING TIMED_WAITING} state
226      * since thread contention monitoring is enabled.
227      * This method returns <tt>-1</tt> if thread contention monitoring
228      * is disabled.
229      *
230      * <p>The Java virtual machine may measure the time with a high
231      * resolution timer. This statistic is reset when
232      * the thread contention monitoring is reenabled.
233      *
234      * @return the approximate accumulated elapsed time in milliseconds
235      * that a thread has been in the <tt>WAITING</tt> or
236      * <tt>TIMED_WAITING</tt> state;
237      * <tt>-1</tt> if thread contention monitoring is disabled.
238      *
239      * @throws java.lang.UnsupportedOperationException if the Java
240      * virtual machine does not support this operation.
241      *
242      * @see ThreadMXBean#isThreadContentionMonitoringSupported
243      * @see ThreadMXBean#setThreadContentionMonitoringEnabled
244      */

245     public long getWaitedTime() {
246         return waitedTime;
247     }
248
249     /**
250      * Returns the total number of times that
251      * the thread associated with this <tt>ThreadInfo</tt>
252      * waited for notification.
253      * I.e. the number of times that a thread has been
254      * in the {@link java.lang.Thread.State#WAITING WAITING}
255      * or {@link java.lang.Thread.State#TIMED_WAITING TIMED_WAITING} state.
256      *
257      * @return the total number of times that the thread
258      * was in the <tt>WAITING</tt> or <tt>TIMED_WAITING</tt> state.
259      */

260     public long getWaitedCount() {
261         return waitedCount;
262     }
263
264     /**
265      * Returns the string representation of the monitor lock that
266      * the thread associated with this <tt>ThreadInfo</tt>
267      * is blocked to enter or waiting to be notified through
268      * the {@link Object#wait Object.wait} method.
269      * The returned string representation of a monitor lock consists of
270      * the name of the class of which the object is an instance, the
271      * at-sign character `@', and the unsigned hexadecimal representation
272      * of the <em>identity</em> hash code of the object.
273      * The returned string may not
274      * be unique depending on the implementation of the
275      * {@link System#identityHashCode} method.
276      * This method returns a string equals to the value of:
277      * <blockquote>
278      * <pre>
279      * lock.getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(lock))
280      * </pre></blockquote>
281      * where <tt>lock</tt> is the monitor lock object.
282      *
283      * <p>If the thread is not blocking to enter on any monitor object,
284      * or is not waiting on a monitor object for notification in a
285      * <tt>Object.wait</tt> call,
286      * this method returns <tt>null</tt>.
287      *
288      * @return the string representation of the monitor lock that
289      * the thread is blocking to enter or waiting to be notified through
290      * the <tt>Object.wait</tt> method if any;
291      * <tt>null</tt> otherwise.
292      *
293      */

294     public String JavaDoc getLockName() {
295         return lockName;
296     }
297
298     /**
299      * Returns the ID of the thread which holds the monitor lock of an object
300      * on which the thread associated with this <tt>ThreadInfo</tt>
301      * is blocking.
302      * This method will return <tt>-1</tt> if this thread is not blocked
303      * or waiting on any monitor, or if the monitor lock is not held
304      * by any thread.
305      *
306      * @return the thread ID of the owner thread of the monitor lock of the
307      * object this thread is blocking on;
308      * <tt>-1</tt> if this thread is not blocked
309      * or waiting on any monitor, or if the monitor lock is not held
310      * by any thread.
311      *
312      * @see #getLockName
313      */

314     public long getLockOwnerId() {
315         return lockOwnerId;
316     }
317
318     /**
319      * Returns the name of the thread which holds the monitor lock of an object
320      * on which the thread associated with this <tt>ThreadInfo</tt>
321      * is blocking.
322      * This method will return <tt>null</tt> if this thread is not blocked
323      * or waiting on any monitor, or if the monitor lock is not held
324      * by any thread.
325      *
326      * @return the name of the thread that holds the monitor lock of the object
327      * this thread is blocking on;
328      * <tt>null</tt> if this thread is not blocked
329      * or waiting on any monitor, or if the monitor lock is not held
330      * by any thread.
331      *
332      * @see #getLockName
333      */

334     public String JavaDoc getLockOwnerName() {
335         return lockOwnerName;
336     }
337
338     /**
339      * Returns the stack trace of the thread
340      * associated with this <tt>ThreadInfo</tt>.
341      * If no stack trace was requested for this thread info, this method
342      * will return a zero-length array.
343      * If the returned array is of non-zero length then the first element of
344      * the array represents the top of the stack, which is the most recent
345      * method invocation in the sequence. The last element of the array
346      * represents the bottom of the stack, which is the least recent method
347      * invocation in the sequence.
348      *
349      * <p>Some Java virtual machines may, under some circumstances, omit one
350      * or more stack frames from the stack trace. In the extreme case,
351      * a virtual machine that has no stack trace information concerning
352      * the thread associated with this <tt>ThreadInfo</tt>
353      * is permitted to return a zero-length array from this method.
354      *
355      * @return an array of <tt>StackTraceElement</tt> objects of the thread.
356      */

357     public StackTraceElement JavaDoc[] getStackTrace() {
358         if (stackTrace == null) {
359             return NO_STACK_TRACE;
360         } else {
361             return stackTrace;
362         }
363     }
364
365     /**
366      * Tests if the thread associated with this <tt>ThreadInfo</tt>
367      * is suspended. This method returns <tt>true</tt> if
368      * {@link Thread#suspend} has been called.
369      *
370      * @return <tt>true</tt> if the thread is suspended;
371      * <tt>false</tt> otherwise.
372      */

373     public boolean isSuspended() {
374          return suspended;
375     }
376
377     /**
378      * Tests if the thread associated with this <tt>ThreadInfo</tt>
379      * is executing native code via the Java Native Interface (JNI).
380      * The JNI native code does not include
381      * the virtual machine support code or the compiled native
382      * code generated by the virtual machine.
383      *
384      * @return <tt>true</tt> if the thread is executing native code;
385      * <tt>false</tt> otherwise.
386      */

387     public boolean isInNative() {
388          return inNative;
389     }
390
391     /**
392      * Returns a string representation of this thread info.
393      *
394      * @return a string representation of this thread info.
395      */

396     public String JavaDoc toString() {
397         return "Thread " + getThreadName() + " (Id = " + getThreadId() + ") " +
398                getThreadState() + " " + getLockName();
399     }
400
401     /**
402      * Returns a <tt>ThreadInfo</tt> object represented by the
403      * given <tt>CompositeData</tt>.
404      * The given <tt>CompositeData</tt> must contain the following attributes:
405      * <blockquote>
406      * <table border>
407      * <tr>
408      * <th align=left>Attribute Name</th>
409      * <th align=left>Type</th>
410      * </tr>
411      * <tr>
412      * <td>threadId</td>
413      * <td><tt>java.lang.Long</tt></td>
414      * </tr>
415      * <tr>
416      * <td>threadName</td>
417      * <td><tt>java.lang.String</tt></td>
418      * </tr>
419      * <tr>
420      * <td>threadState</td>
421      * <td><tt>java.lang.String</tt></td>
422      * </tr>
423      * <tr>
424      * <td>suspended</td>
425      * <td><tt>java.lang.Boolean</tt></td>
426      * </tr>
427      * <tr>
428      * <td>inNative</td>
429      * <td><tt>java.lang.Boolean</tt></td>
430      * </tr>
431      * <tr>
432      * <td>blockedCount</td>
433      * <td><tt>java.lang.Long</tt></td>
434      * </tr>
435      * <tr>
436      * <td>blockedTime</td>
437      * <td><tt>java.lang.Long</tt></td>
438      * </tr>
439      * <tr>
440      * <td>waitedCount</td>
441      * <td><tt>java.lang.Long</tt></td>
442      * </tr>
443      * <tr>
444      * <td>waitedTime</td>
445      * <td><tt>java.lang.Long</tt></td>
446      * </tr>
447      * <tr>
448      * <td>lockName</td>
449      * <td><tt>java.lang.String</tt></td>
450      * </tr>
451      * <tr>
452      * <td>lockOwnerId</td>
453      * <td><tt>java.lang.Long</tt></td>
454      * </tr>
455      * <tr>
456      * <td>lockOwnerName</td>
457      * <td><tt>java.lang.String</tt></td>
458      * </tr>
459      * <tr>
460      * <td>stackTrace</td>
461      * <td><tt>javax.management.openmbean.CompositeData[]</tt>
462      * <p>
463      * Each element is a <tt>CompositeData</tt> representing
464      * StackTraceElement containing the following attributes:
465      * <blockquote>
466      * <table cellspacing=1 cellpadding=0>
467      * <tr>
468      * <th align=left>Attribute Name</th>
469      * <th align=left>Type</th>
470      * </tr>
471      * <tr>
472      * <td>className</td>
473      * <td><tt>java.lang.String</tt></td>
474      * </tr>
475      * <tr>
476      * <td>methodName</td>
477      * <td><tt>java.lang.String</tt></td>
478      * </tr>
479      * <tr>
480      * <td>fileName</td>
481      * <td><tt>java.lang.String</tt></td>
482      * </tr>
483      * <tr>
484      * <td>lineNumber</td>
485      * <td><tt>java.lang.Integer</tt></td>
486      * </tr>
487      * <tr>
488      * <td>nativeMethod</td>
489      * <td><tt>java.lang.Boolean</tt></td>
490      * </tr>
491      * </table>
492      * </blockquote>
493      * </td>
494      * </tr>
495      * </table>
496      * </blockquote>
497      *
498      * @param cd <tt>CompositeData</tt> representing a <tt>ThreadInfo</tt>
499      *
500      * @throws IllegalArgumentException if <tt>cd</tt> does not
501      * represent a <tt>ThreadInfo</tt> with the attributes described
502      * above.
503
504      * @return a <tt>ThreadInfo</tt> object represented
505      * by <tt>cd</tt> if <tt>cd</tt> is not <tt>null</tt>;
506      * <tt>null</tt> otherwise.
507      */

508     public static ThreadInfo JavaDoc from(CompositeData JavaDoc cd) {
509         if (cd == null) {
510             return null;
511         }
512
513         if (cd instanceof ThreadInfoCompositeData) {
514             return ((ThreadInfoCompositeData) cd).getThreadInfo();
515         } else {
516             return new ThreadInfo JavaDoc(cd);
517         }
518     }
519
520     private static final StackTraceElement JavaDoc[] NO_STACK_TRACE =
521         new StackTraceElement JavaDoc[0];
522 }
523
Popular Tags