J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package java.lang.management; |
| 27 | |
| 28 | import javax.management.openmbean.CompositeData; |
| 29 | import sun.management.ThreadInfoCompositeData; |
| 30 | import static java.lang.Thread.State.*; |
| 31 | |
| 32 | /** |
| 33 | * Thread information. <tt>ThreadInfo</tt> contains the information |
| 34 | * about a thread including: |
| 35 | * <h4>General thread information</h4> |
| 36 | * <ul> |
| 37 | * <li>Thread ID.</li> |
| 38 | * <li>Name of the thread.</li> |
| 39 | * </ul> |
| 40 | * |
| 41 | * <h4>Execution information</h4> |
| 42 | * <ul> |
| 43 | * <li>Thread state.</li> |
| 44 | * <li>The object upon which the thread is blocked due to: |
| 45 | * <ul> |
| 46 | * <li>waiting to enter a synchronization block/method, or</li> |
| 47 | * <li>waiting to be notified in a {@link Object#wait Object.wait} method, |
| 48 | * or</li> |
| 49 | * <li>parking due to a {@link java.util.concurrent.locks.LockSupport#park |
| 50 | * LockSupport.park} call.</li> |
| 51 | * </ul> |
| 52 | * </li> |
| 53 | * <li>The ID of the thread that owns the object |
| 54 | * that the thread is blocked.</li> |
| 55 | * <li>Stack trace of the thread.</li> |
| 56 | * <li>List of object monitors locked by the thread.</li> |
| 57 | * <li>List of <a href="LockInfo.html#OwnableSynchronizer"> |
| 58 | * ownable synchronizers</a> locked by the thread.</li> |
| 59 | * </ul> |
| 60 | * |
| 61 | * <h4><a name="SyncStats">Synchronization Statistics</a></h4> |
| 62 | * <ul> |
| 63 | * <li>The number of times that the thread has blocked for |
| 64 | * synchronization or waited for notification.</li> |
| 65 | * <li>The accumulated elapsed time that the thread has blocked |
| 66 | * for synchronization or waited for notification |
| 67 | * since {@link ThreadMXBean#setThreadContentionMonitoringEnabled |
| 68 | * thread contention monitoring} |
| 69 | * was enabled. Some Java virtual machine implementation |
| 70 | * may not support this. The |
| 71 | * {@link ThreadMXBean#isThreadContentionMonitoringSupported()} |
| 72 | * method can be used to determine if a Java virtual machine |
| 73 | * supports this.</li> |
| 74 | * </ul> |
| 75 | * |
| 76 | * <p>This thread information class is designed for use in monitoring of |
| 77 | * the system, not for synchronization control. |
| 78 | * |
| 79 | * <h4>MXBean Mapping</h4> |
| 80 | * <tt>ThreadInfo</tt> is mapped to a {@link CompositeData CompositeData} |
| 81 | * with attributes as specified in |
| 82 | * the {@link #from from} method. |
| 83 | * |
| 84 | * @see ThreadMXBean#getThreadInfo |
| 85 | * @see ThreadMXBean#dumpAllThreads |
| 86 | * |
| 87 | * @author Mandy Chung |
| 88 | * @since 1.5 |
| 89 | */ |
| 90 | |
| 91 | public class ThreadInfo { |
| 92 | private String threadName; |
| 93 | private long threadId; |
| 94 | private long blockedTime; |
| 95 | private long blockedCount; |
| 96 | private long waitedTime; |
| 97 | private long waitedCount; |
| 98 | private LockInfo lock; |
| 99 | private String lockName; |
| 100 | private long lockOwnerId; |
| 101 | private String lockOwnerName; |
| 102 | private boolean inNative; |
| 103 | private boolean suspended; |
| 104 | private Thread.State threadState; |
| 105 | private StackTraceElement[] stackTrace; |
| 106 | private MonitorInfo[] lockedMonitors; |
| 107 | private LockInfo[] lockedSynchronizers; |
| 108 | |
| 109 | private static MonitorInfo[] EMPTY_MONITORS = new MonitorInfo[0]; |
| 110 | private static LockInfo[] EMPTY_SYNCS = new LockInfo[0]; |
| 111 | |
| 112 | /** |
| 113 | * Constructor of ThreadInfo created by the JVM |
| 114 | * |
| 115 | * @param t Thread |
| 116 | * @param state Thread state |
| 117 | * @param lockObj Object on which the thread is blocked |
| 118 | * @param lockOwner the thread holding the lock |
| 119 | * @param blockedCount Number of times blocked to enter a lock |
| 120 | * @param blockedTime Approx time blocked to enter a lock |
| 121 | * @param waitedCount Number of times waited on a lock |
| 122 | * @param waitedTime Approx time waited on a lock |
| 123 | * @param stackTrace Thread stack trace |
| 124 | */ |
| 125 | private ThreadInfo(Thread t, int state, Object lockObj, Thread lockOwner, |
| 126 | long blockedCount, long blockedTime, |
| 127 | long waitedCount, long waitedTime, |
| 128 | StackTraceElement[] stackTrace) { |
| 129 | initialize(t, state, lockObj, lockOwner, |
| 130 | blockedCount, blockedTime, |
| 131 | waitedCount, waitedTime, stackTrace, |
| 132 | EMPTY_MONITORS, EMPTY_SYNCS); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Constructor of ThreadInfo created by the JVM |
| 137 | * for {@link ThreadMXBean#getThreadInfo(long[],boolean,boolean)} |
| 138 | * and {@link ThreadMXBean#dumpAllThreads} |
| 139 | * |
| 140 | * @param t Thread |
| 141 | * @param state Thread state |
| 142 | * @param lockObj Object on which the thread is blocked |
| 143 | * @param lockOwner the thread holding the lock |
| 144 | * @param blockedCount Number of times blocked to enter a lock |
| 145 | * @param blockedTime Approx time blocked to enter a lock |
| 146 | * @param waitedCount Number of times waited on a lock |
| 147 | * @param waitedTime Approx time waited on a lock |
| 148 | * @param stackTrace Thread stack trace |
| 149 | * @param lockedMonitors List of locked monitors |
| 150 | * @param lockedSynchronizers List of locked synchronizers |
| 151 | */ |
| 152 | private ThreadInfo(Thread t, int state, Object lockObj, Thread lockOwner, |
| 153 | long blockedCount, long blockedTime, |
| 154 | long waitedCount, long waitedTime, |
| 155 | StackTraceElement[] stackTrace, |
| 156 | Object[] monitors, |
| 157 | int[] stackDepths, |
| 158 | Object[] synchronizers) { |
| 159 | int numMonitors = (monitors == null ? 0 : monitors.length); |
| 160 | MonitorInfo[] lockedMonitors; |
| 161 | if (numMonitors == 0) { |
| 162 | lockedMonitors = EMPTY_MONITORS; |
| 163 | } else { |
| 164 | lockedMonitors = new MonitorInfo[numMonitors]; |
| 165 | for (int i = 0; i < numMonitors; i++) { |
| 166 | Object lock = monitors[i]; |
| 167 | String className = lock.getClass().getName(); |
| 168 | int identityHashCode = System.identityHashCode(lock); |
| 169 | int depth = stackDepths[i]; |
| 170 | StackTraceElement ste = (depth >= 0 ? stackTrace[depth] |
| 171 | : null); |
| 172 | lockedMonitors[i] = new MonitorInfo(className, |
| 173 | identityHashCode, |
| 174 | depth, |
| 175 | ste); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | int numSyncs = (synchronizers == null ? 0 : synchronizers.length); |
| 180 | LockInfo[] lockedSynchronizers; |
| 181 | if (numSyncs == 0) { |
| 182 | lockedSynchronizers = EMPTY_SYNCS; |
| 183 | } else { |
| 184 | lockedSynchronizers = new LockInfo[numSyncs]; |
| 185 | for (int i = 0; i < numSyncs; i++) { |
| 186 | Object lock = synchronizers[i]; |
| 187 | String className = lock.getClass().getName(); |
| 188 | int identityHashCode = System.identityHashCode(lock); |
| 189 | lockedSynchronizers[i] = new LockInfo(className, |
| 190 | identityHashCode); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | initialize(t, state, lockObj, lockOwner, |
| 195 | blockedCount, blockedTime, |
| 196 | waitedCount, waitedTime, stackTrace, |
| 197 | lockedMonitors, lockedSynchronizers); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Initialize ThreadInfo object |
| 202 | * |
| 203 | * @param t Thread |
| 204 | * @param state Thread state |
| 205 | * @param lockObj Object on which the thread is blocked |
| 206 | * @param lockOwner the thread holding the lock |
| 207 | * @param blockedCount Number of times blocked to enter a lock |
| 208 | * @param blockedTime Approx time blocked to enter a lock |
| 209 | * @param waitedCount Number of times waited on a lock |
| 210 | * @param waitedTime Approx time waited on a lock |
| 211 | * @param stackTrace Thread stack trace |
| 212 | * @param lockedMonitors List of locked monitors |
| 213 | * @param lockedSynchronizers List of locked synchronizers |
| 214 | */ |
| 215 | private void initialize(Thread t, int state, Object lockObj, Thread lockOwner, |
| 216 | long blockedCount, long blockedTime, |
| 217 | long waitedCount, long waitedTime, |
| 218 | StackTraceElement[] stackTrace, |
| 219 | MonitorInfo[] lockedMonitors, |
| 220 | LockInfo[] lockedSynchronizers) { |
| 221 | this.threadId = t.getId(); |
| 222 | this.threadName = t.getName(); |
| 223 | this.threadState = |
| 224 | sun.management.ManagementFactory.toThreadState(state); |
| 225 | this.suspended = |
| 226 | sun.management.ManagementFactory.isThreadSuspended(state); |
| 227 | this.inNative = |
| 228 | sun.management.ManagementFactory.isThreadRunningNative(state); |
| 229 | this.blockedCount = blockedCount; |
| 230 | this.blockedTime = blockedTime; |
| 231 | this.waitedCount = waitedCount; |
| 232 | this.waitedTime = waitedTime; |
| 233 | |
| 234 | if (lockObj == null) { |
| 235 | this.lock = null; |
| 236 | this.lockName = null; |
| 237 | } else { |
| 238 | this.lock = new LockInfo(lockObj); |
| 239 | this.lockName = |
| 240 | lock.getClassName() + '@' + |
| 241 | Integer.toHexString(lock.getIdentityHashCode()); |
| 242 | } |
| 243 | if (lockOwner == null) { |
| 244 | this.lockOwnerId = -1; |
| 245 | this.lockOwnerName = null; |
| 246 | } else { |
| 247 | this.lockOwnerId = lockOwner.getId(); |
| 248 | this.lockOwnerName = lockOwner.getName(); |
| 249 | } |
| 250 | if (stackTrace == null) { |
| 251 | this.stackTrace = NO_STACK_TRACE; |
| 252 | } else { |
| 253 | this.stackTrace = stackTrace; |
| 254 | } |
| 255 | this.lockedMonitors = lockedMonitors; |
| 256 | this.lockedSynchronizers = lockedSynchronizers; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Constructs a <tt>ThreadInfo</tt> object from a |
| 261 | * {@link CompositeData CompositeData}. |
| 262 | */ |
| 263 | private ThreadInfo(CompositeData cd) { |
| 264 | ThreadInfoCompositeData ticd = ThreadInfoCompositeData.getInstance(cd); |
| 265 | |
| 266 | threadId = ticd.threadId(); |
| 267 | threadName = ticd.threadName(); |
| 268 | blockedTime = ticd.blockedTime(); |
| 269 | blockedCount = ticd.blockedCount(); |
| 270 | waitedTime = ticd.waitedTime(); |
| 271 | waitedCount = ticd.waitedCount(); |
| 272 | lockName = ticd.lockName(); |
| 273 | lockOwnerId = ticd.lockOwnerId(); |
| 274 | lockOwnerName = ticd.lockOwnerName(); |
| 275 | threadState = ticd.threadState(); |
| 276 | suspended = ticd.suspended(); |
| 277 | inNative = ticd.inNative(); |
| 278 | stackTrace = ticd.stackTrace(); |
| 279 | |
| 280 | // 6.0 attributes |
| 281 | if (ticd.isCurrentVersion()) { |
| 282 | lock = ticd.lockInfo(); |
| 283 | lockedMonitors = ticd.lockedMonitors(); |
| 284 | lockedSynchronizers = ticd.lockedSynchronizers(); |
| 285 | } else { |
| 286 | // lockInfo is a new attribute added in 1.6 ThreadInfo |
| 287 | // If cd is a 5.0 version, construct the LockInfo object |
| 288 | // from the lockName value. |
| 289 | if (lockName != null) { |
| 290 | String result[] = lockName.split("@"); |
| 291 | if (result.length == 2) { |
| 292 | int identityHashCode = Integer.parseInt(result[1], 16); |
| 293 | lock = new LockInfo(result[0], identityHashCode); |
| 294 | } else { |
| 295 | assert result.length == 2; |
| 296 | lock = null; |
| 297 | } |
| 298 | } else { |
| 299 | lock = null; |
| 300 | } |
| 301 | lockedMonitors = EMPTY_MONITORS; |
| 302 | lockedSynchronizers = EMPTY_SYNCS; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Returns the ID of the thread associated with this <tt>ThreadInfo</tt>. |
| 308 | * |
| 309 | * @return the ID of the associated thread. |
| 310 | */ |
| 311 | public long getThreadId() { |
| 312 | return threadId; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Returns the name of the thread associated with this <tt>ThreadInfo</tt>. |
| 317 | * |
| 318 | * @return the name of the associated thread. |
| 319 | */ |
| 320 | public String getThreadName() { |
| 321 | return threadName; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Returns the state of the thread associated with this <tt>ThreadInfo</tt>. |
| 326 | * |
| 327 | * @return <tt>Thread.State</tt> of the associated thread. |
| 328 | */ |
| 329 | public Thread.State getThreadState() { |
| 330 | return threadState; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Returns the approximate accumulated elapsed time (in milliseconds) |
| 335 | * that the thread associated with this <tt>ThreadInfo</tt> |
| 336 | * has blocked to enter or reenter a monitor |
| 337 | * since thread contention monitoring is enabled. |
| 338 | * I.e. the total accumulated time the thread has been in the |
| 339 | * {@link java.lang.Thread.State#BLOCKED BLOCKED} state since thread |
| 340 | * contention monitoring was last enabled. |
| 341 | * This method returns <tt>-1</tt> if thread contention monitoring |
| 342 | * is disabled. |
| 343 | * |
| 344 | * <p>The Java virtual machine may measure the time with a high |
| 345 | * resolution timer. This statistic is reset when |
| 346 | * the thread contention monitoring is reenabled. |
| 347 | * |
| 348 | * @return the approximate accumulated elapsed time in milliseconds |
| 349 | * that a thread entered the <tt>BLOCKED</tt> state; |
| 350 | * <tt>-1</tt> if thread contention monitoring is disabled. |
| 351 | * |
| 352 | * @throws java.lang.UnsupportedOperationException if the Java |
| 353 | * virtual machine does not support this operation. |
| 354 | * |
| 355 | * @see ThreadMXBean#isThreadContentionMonitoringSupported |
| 356 | * @see ThreadMXBean#setThreadContentionMonitoringEnabled |
| 357 | */ |
| 358 | public long getBlockedTime() { |
| 359 | return blockedTime; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Returns the total number of times that |
| 364 | * the thread associated with this <tt>ThreadInfo</tt> |
| 365 | * blocked to enter or reenter a monitor. |
| 366 | * I.e. the number of times a thread has been in the |
| 367 | * {@link java.lang.Thread.State#BLOCKED BLOCKED} state. |
| 368 | * |
| 369 | * @return the total number of times that the thread |
| 370 | * entered the <tt>BLOCKED</tt> state. |
| 371 | */ |
| 372 | public long getBlockedCount() { |
| 373 | return blockedCount; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Returns the approximate accumulated elapsed time (in milliseconds) |
| 378 | * that the thread associated with this <tt>ThreadInfo</tt> |
| 379 | * has waited for notification |
| 380 | * since thread contention monitoring is enabled. |
| 381 | * I.e. the total accumulated time the thread has been in the |
| 382 | * {@link java.lang.Thread.State#WAITING WAITING} |
| 383 | * or {@link java.lang.Thread.State#TIMED_WAITING TIMED_WAITING} state |
| 384 | * since thread contention monitoring is enabled. |
| 385 | * This method returns <tt>-1</tt> if thread contention monitoring |
| 386 | * is disabled. |
| 387 | * |
| 388 | * <p>The Java virtual machine may measure the time with a high |
| 389 | * resolution timer. This statistic is reset when |
| 390 | * the thread contention monitoring is reenabled. |
| 391 | * |
| 392 | * @return the approximate accumulated elapsed time in milliseconds |
| 393 | * that a thread has been in the <tt>WAITING</tt> or |
| 394 | * <tt>TIMED_WAITING</tt> state; |
| 395 | * <tt>-1</tt> if thread contention monitoring is disabled. |
| 396 | * |
| 397 | * @throws java.lang.UnsupportedOperationException if the Java |
| 398 | * virtual machine does not support this operation. |
| 399 | * |
| 400 | * @see ThreadMXBean#isThreadContentionMonitoringSupported |
| 401 | * @see ThreadMXBean#setThreadContentionMonitoringEnabled |
| 402 | */ |
| 403 | public long getWaitedTime() { |
| 404 | return waitedTime; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Returns the total number of times that |
| 409 | * the thread associated with this <tt>ThreadInfo</tt> |
| 410 | * waited for notification. |
| 411 | * I.e. the number of times that a thread has been |
| 412 | * in the {@link java.lang.Thread.State#WAITING WAITING} |
| 413 | * or {@link java.lang.Thread.State#TIMED_WAITING TIMED_WAITING} state. |
| 414 | * |
| 415 | * @return the total number of times that the thread |
| 416 | * was in the <tt>WAITING</tt> or <tt>TIMED_WAITING</tt> state. |
| 417 | */ |
| 418 | public long getWaitedCount() { |
| 419 | return waitedCount; |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Returns the <tt>LockInfo</tt> of an object for which |
| 424 | * the thread associated with this <tt>ThreadInfo</tt> |
| 425 | * is blocked waiting. |
| 426 | * A thread can be blocked waiting for one of the following: |
| 427 | * <ul> |
| 428 | * <li>an object monitor to be acquired for entering or reentering |
| 429 | * a synchronization block/method. |
| 430 | * <br>The thread is in the {@link java.lang.Thread.State#BLOCKED BLOCKED} |
| 431 | * state waiting to enter the <tt>synchronized</tt> statement |
| 432 | * or method. |
| 433 | * <p></li> |
| 434 | * <li>an object monitor to be notified by another thread. |
| 435 | * <br>The thread is in the {@link java.lang.Thread.State#WAITING WAITING} |
| 436 | * or {@link java.lang.Thread.State#TIMED_WAITING TIMED_WAITING} state |
| 437 | * due to a call to the {@link Object#wait Object.wait} method. |
| 438 | * <p></li> |
| 439 | * <li>a synchronization object responsible for the thread parking. |
| 440 | * <br>The thread is in the {@link java.lang.Thread.State#WAITING WAITING} |
| 441 | * or {@link java.lang.Thread.State#TIMED_WAITING TIMED_WAITING} state |
| 442 | * due to a call to the |
| 443 | * {@link java.util.concurrent.locks.LockSupport#park(Object) |
| 444 | * LockSupport.park} method. The synchronization object |
| 445 | * is the object returned from |
| 446 | * {@link java.util.concurrent.locks.LockSupport#getBlocker |
| 447 | * LockSupport.getBlocker} method. Typically it is an |
| 448 | * <a href="LockInfo.html#OwnableSynchronizer"> ownable synchronizer</a> |
| 449 | * or a {@link java.util.concurrent.locks.Condition Condition}.</li> |
| 450 | * </ul> |
| 451 | * |
| 452 | * <p>This method returns <tt>null</tt> if the thread is not in any of |
| 453 | * the above conditions. |
| 454 | * |
| 455 | * @return <tt>LockInfo</tt> of an object for which the thread |
| 456 | * is blocked waiting if any; <tt>null</tt> otherwise. |
| 457 | * @since 1.6 |
| 458 | */ |
| 459 | public LockInfo getLockInfo() { |
| 460 | return lock; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Returns the {@link LockInfo#toString string representation} |
| 465 | * of an object for which the thread associated with this |
| 466 | * <tt>ThreadInfo</tt> is blocked waiting. |
| 467 | * This method is equivalent to calling: |
| 468 | * <blockquote> |
| 469 | * <pre> |
| 470 | * getLockInfo().toString() |
| 471 | * </pre></blockquote> |
| 472 | * |
| 473 | * <p>This method will return <tt>null</tt> if this thread is not blocked |
| 474 | * waiting for any object or if the object is not owned by any thread. |
| 475 | * |
| 476 | * @return the string representation of the object on which |
| 477 | * the thread is blocked if any; |
| 478 | * <tt>null</tt> otherwise. |
| 479 | * |
| 480 | * @see #getLockInfo |
| 481 | */ |
| 482 | public String getLockName() { |
| 483 | return lockName; |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Returns the ID of the thread which owns the object |
| 488 | * for which the thread associated with this <tt>ThreadInfo</tt> |
| 489 | * is blocked waiting. |
| 490 | * This method will return <tt>-1</tt> if this thread is not blocked |
| 491 | * waiting for any object or if the object is not owned by any thread. |
| 492 | * |
| 493 | * @return the thread ID of the owner thread of the object |
| 494 | * this thread is blocked on; |
| 495 | * <tt>-1</tt> if this thread is not blocked |
| 496 | * or if the object lis not owned by any thread. |
| 497 | * |
| 498 | * @see #getLockInfo |
| 499 | */ |
| 500 | public long getLockOwnerId() { |
| 501 | return lockOwnerId; |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Returns the name of the thread which owns the object |
| 506 | * for which the thread associated with this <tt>ThreadInfo</tt> |
| 507 | * is blocked waiting. |
| 508 | * This method will return <tt>null</tt> if this thread is not blocked |
| 509 | * waiting for any object or if the object is not owned by any thread. |
| 510 | * |
| 511 | * @return the name of the thread that owns the object |
| 512 | * this thread is blocked on; |
| 513 | * <tt>null</tt> if this thread is not blocked |
| 514 | * or if the object is not owned by any thread. |
| 515 | * |
| 516 | * @see #getLockInfo |
| 517 | */ |
| 518 | public String getLockOwnerName() { |
| 519 | return lockOwnerName; |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Returns the stack trace of the thread |
| 524 | * associated with this <tt>ThreadInfo</tt>. |
| 525 | * If no stack trace was requested for this thread info, this method |
| 526 | * will return a zero-length array. |
| 527 | * If the returned array is of non-zero length then the first element of |
| 528 | * the array represents the top of the stack, which is the most recent |
| 529 | * method invocation in the sequence. The last element of the array |
| 530 | * represents the bottom of the stack, which is the least recent method |
| 531 | * invocation in the sequence. |
| 532 | * |
| 533 | * <p>Some Java virtual machines may, under some circumstances, omit one |
| 534 | * or more stack frames from the stack trace. In the extreme case, |
| 535 | * a virtual machine that has no stack trace information concerning |
| 536 | * the thread associated with this <tt>ThreadInfo</tt> |
| 537 | * is permitted to return a zero-length array from this method. |
| 538 | * |
| 539 | * @return an array of <tt>StackTraceElement</tt> objects of the thread. |
| 540 | */ |
| 541 | public StackTraceElement[] getStackTrace() { |
| 542 | return stackTrace; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Tests if the thread associated with this <tt>ThreadInfo</tt> |
| 547 | * is suspended. This method returns <tt>true</tt> if |
| 548 | * {@link Thread#suspend} has been called. |
| 549 | * |
| 550 | * @return <tt>true</tt> if the thread is suspended; |
| 551 | * <tt>false</tt> otherwise. |
| 552 | */ |
| 553 | public boolean isSuspended() { |
| 554 | return suspended; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Tests if the thread associated with this <tt>ThreadInfo</tt> |
| 559 | * is executing native code via the Java Native Interface (JNI). |
| 560 | * The JNI native code does not include |
| 561 | * the virtual machine support code or the compiled native |
| 562 | * code generated by the virtual machine. |
| 563 | * |
| 564 | * @return <tt>true</tt> if the thread is executing native code; |
| 565 | * <tt>false</tt> otherwise. |
| 566 | */ |
| 567 | public boolean isInNative() { |
| 568 | return inNative; |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Returns a string representation of this thread info. |
| 573 | * The format of this string depends on the implementation. |
| 574 | * The returned string will typically include |
| 575 | * the {@linkplain #getThreadName thread name}, |
| 576 | * the {@linkplain #getThreadId thread ID}, |
| 577 | * its {@linkplain #getThreadState state}, |
| 578 | * and a {@linkplain #getStackTrace stack trace} if any. |
| 579 | * |
| 580 | * @return a string representation of this thread info. |
| 581 | */ |
| 582 | public String toString() { |
| 583 | StringBuilder sb = new StringBuilder("\"" + getThreadName() + "\"" + |
| 584 | " Id=" + getThreadId() + " " + |
| 585 | getThreadState()); |
| 586 | if (getLockName() != null) { |
| 587 | sb.append(" on " + getLockName()); |
| 588 | } |
| 589 | if (getLockOwnerName() != null) { |
| 590 | sb.append(" owned by \"" + getLockOwnerName() + |
| 591 | "\" Id=" + getLockOwnerId()); |
| 592 | } |
| 593 | if (isSuspended()) { |
| 594 | sb.append(" (suspended)"); |
| 595 | } |
| 596 | if (isInNative()) { |
| 597 | sb.append(" (in native)"); |
| 598 | } |
| 599 | sb.append('\n'); |
| 600 | int i = 0; |
| 601 | for (; i < stackTrace.length && i < MAX_FRAMES; i++) { |
| 602 | StackTraceElement ste = stackTrace[i]; |
| 603 | sb.append("\tat " + ste.toString()); |
| 604 | sb.append('\n'); |
| 605 | if (i == 0 && getLockInfo() != null) { |
| 606 | Thread.State ts = getThreadState(); |
| 607 | switch (ts) { |
| 608 | case BLOCKED: |
| 609 | sb.append("\t- blocked on " + getLockInfo()); |
| 610 | sb.append('\n'); |
| 611 | break; |
| 612 | case WAITING: |
| 613 | sb.append("\t- waiting on " + getLockInfo()); |
| 614 | sb.append('\n'); |
| 615 | break; |
| 616 | case TIMED_WAITING: |
| 617 | sb.append("\t- waiting on " + getLockInfo()); |
| 618 | sb.append('\n'); |
| 619 | break; |
| 620 | default: |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | for (MonitorInfo mi : lockedMonitors) { |
| 625 | if (mi.getLockedStackDepth() == i) { |
| 626 | sb.append("\t- locked " + mi); |
| 627 | sb.append('\n'); |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | if (i < stackTrace.length) { |
| 632 | sb.append("\t..."); |
| 633 | sb.append('\n'); |
| 634 | } |
| 635 | |
| 636 | LockInfo[] locks = getLockedSynchronizers(); |
| 637 | if (locks.length > 0) { |
| 638 | sb.append("\n\tNumber of locked synchronizers = " + locks.length); |
| 639 | sb.append('\n'); |
| 640 | for (LockInfo li : locks) { |
| 641 | sb.append("\t- " + li); |
| 642 | sb.append('\n'); |
| 643 | } |
| 644 | } |
| 645 | sb.append('\n'); |
| 646 | return sb.toString(); |
| 647 | } |
| 648 | private static final int MAX_FRAMES = 8; |
| 649 | |
| 650 | /** |
| 651 | * Returns a <tt>ThreadInfo</tt> object represented by the |
| 652 | * given <tt>CompositeData</tt>. |
| 653 | * The given <tt>CompositeData</tt> must contain the following attributes |
| 654 | * unless otherwise specified below: |
| 655 | * <blockquote> |
| 656 | * <table border> |
| 657 | * <tr> |
| 658 | * <th align=left>Attribute Name</th> |
| 659 | * <th align=left>Type</th> |
| 660 | * </tr> |
| 661 | * <tr> |
| 662 | * <td>threadId</td> |
| 663 | * <td><tt>java.lang.Long</tt></td> |
| 664 | * </tr> |
| 665 | * <tr> |
| 666 | * <td>threadName</td> |
| 667 | * <td><tt>java.lang.String</tt></td> |
| 668 | * </tr> |
| 669 | * <tr> |
| 670 | * <td>threadState</td> |
| 671 | * <td><tt>java.lang.String</tt></td> |
| 672 | * </tr> |
| 673 | * <tr> |
| 674 | * <td>suspended</td> |
| 675 | * <td><tt>java.lang.Boolean</tt></td> |
| 676 | * </tr> |
| 677 | * <tr> |
| 678 | * <td>inNative</td> |
| 679 | * <td><tt>java.lang.Boolean</tt></td> |
| 680 | * </tr> |
| 681 | * <tr> |
| 682 | * <td>blockedCount</td> |
| 683 | * <td><tt>java.lang.Long</tt></td> |
| 684 | * </tr> |
| 685 | * <tr> |
| 686 | * <td>blockedTime</td> |
| 687 | * <td><tt>java.lang.Long</tt></td> |
| 688 | * </tr> |
| 689 | * <tr> |
| 690 | * <td>waitedCount</td> |
| 691 | * <td><tt>java.lang.Long</tt></td> |
| 692 | * </tr> |
| 693 | * <tr> |
| 694 | * <td>waitedTime</td> |
| 695 | * <td><tt>java.lang.Long</tt></td> |
| 696 | * </tr> |
| 697 | * <tr> |
| 698 | * <td>lockInfo</td> |
| 699 | * <td><tt>javax.management.openmbean.CompositeData</tt> |
| 700 | * - the mapped type for {@link LockInfo} as specified in the |
| 701 | * <a href="../../../javax/management/MXBean.html#mapping-rules"> |
| 702 | * type mapping rules</a> of |
| 703 | * {@linkplain javax.management.MXBean MXBeans}. |
| 704 | * <p> |
| 705 | * If <tt>cd</tt> does not contain this attribute, |
| 706 | * the <tt>LockInfo</tt> object will be constructed from |
| 707 | * the value of the <tt>lockName</tt> attribute. </td> |
| 708 | * </tr> |
| 709 | * <tr> |
| 710 | * <td>lockName</td> |
| 711 | * <td><tt>java.lang.String</tt></td> |
| 712 | * </tr> |
| 713 | * <tr> |
| 714 | * <td>lockOwnerId</td> |
| 715 | * <td><tt>java.lang.Long</tt></td> |
| 716 | * </tr> |
| 717 | * <tr> |
| 718 | * <td>lockOwnerName</td> |
| 719 | * <td><tt>java.lang.String</tt></td> |
| 720 | * </tr> |
| 721 | * <tr> |
| 722 | * <td><a name="StackTrace">stackTrace</a></td> |
| 723 | * <td><tt>javax.management.openmbean.CompositeData[]</tt> |
| 724 | * <p> |
| 725 | * Each element is a <tt>CompositeData</tt> representing |
| 726 | * StackTraceElement containing the following attributes: |
| 727 | * <blockquote> |
| 728 | * <table cellspacing=1 cellpadding=0> |
| 729 | * <tr> |
| 730 | * <th align=left>Attribute Name</th> |
| 731 | * <th align=left>Type</th> |
| 732 | * </tr> |
| 733 | * <tr> |
| 734 | * <td>className</td> |
| 735 | * <td><tt>java.lang.String</tt></td> |
| 736 | * </tr> |
| 737 | * <tr> |
| 738 | * <td>methodName</td> |
| 739 | * <td><tt>java.lang.String</tt></td> |
| 740 | * </tr> |
| 741 | * <tr> |
| 742 | * <td>fileName</td> |
| 743 | * <td><tt>java.lang.String</tt></td> |
| 744 | * </tr> |
| 745 | * <tr> |
| 746 | * <td>lineNumber</td> |
| 747 | * <td><tt>java.lang.Integer</tt></td> |
| 748 | * </tr> |
| 749 | * <tr> |
| 750 | * <td>nativeMethod</td> |
| 751 | * <td><tt>java.lang.Boolean</tt></td> |
| 752 | * </tr> |
| 753 | * </table> |
| 754 | * </blockquote> |
| 755 | * </td> |
| 756 | * </tr> |
| 757 | * <tr> |
| 758 | * <td>lockedMonitors</td> |
| 759 | * <td><tt>javax.management.openmbean.CompositeData[]</tt> |
| 760 | * whose element type is the mapped type for |
| 761 | * {@link MonitorInfo} as specified in the |
| 762 | * {@link MonitorInfo#from Monitor.from} method. |
| 763 | * <p> |
| 764 | * If <tt>cd</tt> does not contain this attribute, |
| 765 | * this attribute will be set to an empty array. </td> |
| 766 | * </tr> |
| 767 | * <tr> |
| 768 | * <td>lockedSynchronizers</td> |
| 769 | * <td><tt>javax.management.openmbean.CompositeData[]</tt> |
| 770 | * whose element type is the mapped type for |
| 771 | * {@link LockInfo} as specified in the |
| 772 | * <a href="../../../javax/management/MXBean.html#mapping-rules"> |
| 773 | * type mapping rules</a> of |
| 774 | * {@linkplain javax.management.MXBean MXBeans}. |
| 775 | * <p> |
| 776 | * If <tt>cd</tt> does not contain this attribute, |
| 777 | * this attribute will be set to an empty array. </td> |
| 778 | * </tr> |
| 779 | * </table> |
| 780 | * </blockquote> |
| 781 | * |
| 782 | * @param cd <tt>CompositeData</tt> representing a <tt>ThreadInfo</tt> |
| 783 | * |
| 784 | * @throws IllegalArgumentException if <tt>cd</tt> does not |
| 785 | * represent a <tt>ThreadInfo</tt> with the attributes described |
| 786 | * above. |
| 787 | * |
| 788 | * @return a <tt>ThreadInfo</tt> object represented |
| 789 | * by <tt>cd</tt> if <tt>cd</tt> is not <tt>null</tt>; |
| 790 | * <tt>null</tt> otherwise. |
| 791 | */ |
| 792 | public static ThreadInfo from(CompositeData cd) { |
| 793 | if (cd == null) { |
| 794 | return null; |
| 795 | } |
| 796 | |
| 797 | if (cd instanceof ThreadInfoCompositeData) { |
| 798 | return ((ThreadInfoCompositeData) cd).getThreadInfo(); |
| 799 | } else { |
| 800 | return new ThreadInfo(cd); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * Returns an array of {@link MonitorInfo} objects, each of which |
| 806 | * represents an object monitor currently locked by the thread |
| 807 | * associated with this <tt>ThreadInfo</tt>. |
| 808 | * If no locked monitor was requested for this thread info or |
| 809 | * no monitor is locked by the thread, this method |
| 810 | * will return a zero-length array. |
| 811 | * |
| 812 | * @return an array of <tt>MonitorInfo</tt> objects representing |
| 813 | * the object monitors locked by the thread. |
| 814 | * |
| 815 | * @since 1.6 |
| 816 | */ |
| 817 | public MonitorInfo[] getLockedMonitors() { |
| 818 | return lockedMonitors; |
| 819 | } |
| 820 | |
| 821 | /** |
| 822 | * Returns an array of {@link LockInfo} objects, each of which |
| 823 | * represents an <a href="LockInfo.html#OwnableSynchronizer">ownable |
| 824 | * synchronizer</a> currently locked by the thread associated with |
| 825 | * this <tt>ThreadInfo</tt>. If no locked synchronizer was |
| 826 | * requested for this thread info or no synchronizer is locked by |
| 827 | * the thread, this method will return a zero-length array. |
| 828 | * |
| 829 | * @return an array of <tt>LockInfo</tt> objects representing |
| 830 | * the ownable synchronizers locked by the thread. |
| 831 | * |
| 832 | * @since 1.6 |
| 833 | */ |
| 834 | public LockInfo[] getLockedSynchronizers() { |
| 835 | // represents an <a href="LockInfo.html#OwnableSynchronizer"> |
| 836 | return lockedSynchronizers; |
| 837 | } |
| 838 | |
| 839 | private static final StackTraceElement[] NO_STACK_TRACE = |
| 840 | new StackTraceElement[0]; |
| 841 | } |