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 java.util.Map; |
| 29 | |
| 30 | /** |
| 31 | * The management interface for the thread system of |
| 32 | * the Java virtual machine. |
| 33 | * |
| 34 | * <p> A Java virtual machine has a single instance of the implementation |
| 35 | * class of this interface. This instance implementing this interface is |
| 36 | * an <a href="ManagementFactory.html#MXBean">MXBean</a> |
| 37 | * that can be obtained by calling |
| 38 | * the {@link ManagementFactory#getThreadMXBean} method or |
| 39 | * from the {@link ManagementFactory#getPlatformMBeanServer |
| 40 | * platform <tt>MBeanServer</tt>} method. |
| 41 | * |
| 42 | * <p>The <tt>ObjectName</tt> for uniquely identifying the MXBean for |
| 43 | * the thread system within an MBeanServer is: |
| 44 | * <blockquote> |
| 45 | * {@link ManagementFactory#THREAD_MXBEAN_NAME |
| 46 | * <tt>java.lang:type=Threading</tt>} |
| 47 | * </blockquote> |
| 48 | * |
| 49 | * <h4>Thread ID</h4> |
| 50 | * Thread ID is a positive long value returned by calling the |
| 51 | * {@link java.lang.Thread#getId} method for a thread. |
| 52 | * The thread ID is unique during its lifetime. When a thread |
| 53 | * is terminated, this thread ID may be reused. |
| 54 | * |
| 55 | * <p> Some methods in this interface take a thread ID or an array |
| 56 | * of thread IDs as the input parameter and return per-thread information. |
| 57 | * |
| 58 | * <h4>Thread CPU time</h4> |
| 59 | * A Java virtual machine implementation may support measuring |
| 60 | * the CPU time for the current thread, for any thread, or for no threads. |
| 61 | * |
| 62 | * <p> |
| 63 | * The {@link #isThreadCpuTimeSupported} method can be used to determine |
| 64 | * if a Java virtual machine supports measuring of the CPU time for any |
| 65 | * thread. The {@link #isCurrentThreadCpuTimeSupported} method can |
| 66 | * be used to determine if a Java virtual machine supports measuring of |
| 67 | * the CPU time for the current thread. |
| 68 | * A Java virtual machine implementation that supports CPU time measurement |
| 69 | * for any thread will also support that for the current thread. |
| 70 | * |
| 71 | * <p> The CPU time provided by this interface has nanosecond precision |
| 72 | * but not necessarily nanosecond accuracy. |
| 73 | * |
| 74 | * <p> |
| 75 | * A Java virtual machine may disable CPU time measurement |
| 76 | * by default. |
| 77 | * The {@link #isThreadCpuTimeEnabled} and {@link #setThreadCpuTimeEnabled} |
| 78 | * methods can be used to test if CPU time measurement is enabled |
| 79 | * and to enable/disable this support respectively. |
| 80 | * Enabling thread CPU measurement could be expensive in some |
| 81 | * Java virtual machine implementations. |
| 82 | * |
| 83 | * <h4>Thread Contention Monitoring</h4> |
| 84 | * Some Java virtual machines may support thread contention monitoring. |
| 85 | * When thread contention monitoring is enabled, the accumulated elapsed |
| 86 | * time that the thread has blocked for synchronization or waited for |
| 87 | * notification will be collected and returned in the |
| 88 | * <a href="ThreadInfo.html#SyncStats"><tt>ThreadInfo</tt></a> object. |
| 89 | * <p> |
| 90 | * The {@link #isThreadContentionMonitoringSupported} method can be used to |
| 91 | * determine if a Java virtual machine supports thread contention monitoring. |
| 92 | * The thread contention monitoring is disabled by default. The |
| 93 | * {@link #setThreadContentionMonitoringEnabled} method can be used to enable |
| 94 | * thread contention monitoring. |
| 95 | * |
| 96 | * <h4>Synchronization Information and Deadlock Detection</h4> |
| 97 | * Some Java virtual machines may support monitoring of |
| 98 | * {@linkplain #isObjectMonitorUsageSupported object monitor usage} and |
| 99 | * {@linkplain #isSynchronizerUsageSupported ownable synchronizer usage}. |
| 100 | * The {@link #getThreadInfo(long[], boolean, boolean)} and |
| 101 | * {@link #dumpAllThreads} methods can be used to obtain the thread stack trace |
| 102 | * and synchronization information including which |
| 103 | * {@linkplain LockInfo <i>lock</i>} a thread is blocked to |
| 104 | * acquire or waiting on and which locks the thread currently owns. |
| 105 | * <p> |
| 106 | * The <tt>ThreadMXBean</tt> interface provides the |
| 107 | * {@link #findMonitorDeadlockedThreads} and |
| 108 | * {@link #findDeadlockedThreads} methods to find deadlocks in |
| 109 | * the running application. |
| 110 | * |
| 111 | * @see <a href="../../../javax/management/package-summary.html"> |
| 112 | * JMX Specification.</a> |
| 113 | * @see <a href="package-summary.html#examples"> |
| 114 | * Ways to Access MXBeans</a> |
| 115 | * |
| 116 | * @author Mandy Chung |
| 117 | * @since 1.5 |
| 118 | */ |
| 119 | |
| 120 | public interface ThreadMXBean { |
| 121 | /** |
| 122 | * Returns the current number of live threads including both |
| 123 | * daemon and non-daemon threads. |
| 124 | * |
| 125 | * @return the current number of live threads. |
| 126 | */ |
| 127 | public int getThreadCount(); |
| 128 | |
| 129 | /** |
| 130 | * Returns the peak live thread count since the Java virtual machine |
| 131 | * started or peak was reset. |
| 132 | * |
| 133 | * @return the peak live thread count. |
| 134 | */ |
| 135 | public int getPeakThreadCount(); |
| 136 | |
| 137 | /** |
| 138 | * Returns the total number of threads created and also started |
| 139 | * since the Java virtual machine started. |
| 140 | * |
| 141 | * @return the total number of threads started. |
| 142 | */ |
| 143 | public long getTotalStartedThreadCount(); |
| 144 | |
| 145 | /** |
| 146 | * Returns the current number of live daemon threads. |
| 147 | * |
| 148 | * @return the current number of live daemon threads. |
| 149 | */ |
| 150 | public int getDaemonThreadCount(); |
| 151 | |
| 152 | /** |
| 153 | * Returns all live thread IDs. |
| 154 | * Some threads included in the returned array |
| 155 | * may have been terminated when this method returns. |
| 156 | * |
| 157 | * @return an array of <tt>long</tt>, each is a thread ID. |
| 158 | * |
| 159 | * @throws java.lang.SecurityException if a security manager |
| 160 | * exists and the caller does not have |
| 161 | * ManagementPermission("monitor"). |
| 162 | */ |
| 163 | public long[] getAllThreadIds(); |
| 164 | |
| 165 | /** |
| 166 | * Returns the thread info for a thread of the specified |
| 167 | * <tt>id</tt> with no stack trace. |
| 168 | * This method is equivalent to calling: |
| 169 | * <blockquote> |
| 170 | * {@link #getThreadInfo(long, int) getThreadInfo(id, 0);} |
| 171 | * </blockquote> |
| 172 | * |
| 173 | * <p> |
| 174 | * This method returns a <tt>ThreadInfo</tt> object representing |
| 175 | * the thread information for the thread of the specified ID. |
| 176 | * The stack trace, locked monitors, and locked synchronizers |
| 177 | * in the returned <tt>ThreadInfo</tt> object will |
| 178 | * be empty. |
| 179 | * |
| 180 | * If a thread of the given ID is not alive or does not exist, |
| 181 | * this method will return <tt>null</tt>. A thread is alive if |
| 182 | * it has been started and has not yet died. |
| 183 | * |
| 184 | * <p> |
| 185 | * <b>MBeanServer access</b>:<br> |
| 186 | * The mapped type of <tt>ThreadInfo</tt> is |
| 187 | * <tt>CompositeData</tt> with attributes as specified in the |
| 188 | * {@link ThreadInfo#from ThreadInfo.from} method. |
| 189 | * |
| 190 | * @param id the thread ID of the thread. Must be positive. |
| 191 | * |
| 192 | * @return a {@link ThreadInfo} object for the thread of the given ID |
| 193 | * with no stack trace, no locked monitor and no synchronizer info; |
| 194 | * <tt>null</tt> if the thread of the given ID is not alive or |
| 195 | * it does not exist. |
| 196 | * |
| 197 | * @throws IllegalArgumentException if <tt>id <= 0</tt>. |
| 198 | * @throws java.lang.SecurityException if a security manager |
| 199 | * exists and the caller does not have |
| 200 | * ManagementPermission("monitor"). |
| 201 | */ |
| 202 | public ThreadInfo getThreadInfo(long id); |
| 203 | |
| 204 | /** |
| 205 | * Returns the thread info for each thread |
| 206 | * whose ID is in the input array <tt>ids</tt> with no stack trace. |
| 207 | * This method is equivalent to calling: |
| 208 | * <blockquote><pre> |
| 209 | * {@link #getThreadInfo(long[], int) getThreadInfo}(ids, 0); |
| 210 | * </pre></blockquote> |
| 211 | * |
| 212 | * <p> |
| 213 | * This method returns an array of the <tt>ThreadInfo</tt> objects. |
| 214 | * The stack trace, locked monitors, and locked synchronizers |
| 215 | * in each <tt>ThreadInfo</tt> object will be empty. |
| 216 | * |
| 217 | * If a thread of a given ID is not alive or does not exist, |
| 218 | * the corresponding element in the returned array will |
| 219 | * contain <tt>null</tt>. A thread is alive if |
| 220 | * it has been started and has not yet died. |
| 221 | * |
| 222 | * <p> |
| 223 | * <b>MBeanServer access</b>:<br> |
| 224 | * The mapped type of <tt>ThreadInfo</tt> is |
| 225 | * <tt>CompositeData</tt> with attributes as specified in the |
| 226 | * {@link ThreadInfo#from ThreadInfo.from} method. |
| 227 | * |
| 228 | * @param ids an array of thread IDs. |
| 229 | * @return an array of the {@link ThreadInfo} objects, each containing |
| 230 | * information about a thread whose ID is in the corresponding |
| 231 | * element of the input array of IDs |
| 232 | * with no stack trace, no locked monitor and no synchronizer info. |
| 233 | * |
| 234 | * @throws IllegalArgumentException if any element in the input array |
| 235 | * <tt>ids</tt> is <tt><= 0</tt>. |
| 236 | * @throws java.lang.SecurityException if a security manager |
| 237 | * exists and the caller does not have |
| 238 | * ManagementPermission("monitor"). |
| 239 | */ |
| 240 | public ThreadInfo[] getThreadInfo(long[] ids); |
| 241 | |
| 242 | /** |
| 243 | * Returns a thread info for a thread of the specified <tt>id</tt>, |
| 244 | * with stack trace of a specified number of stack trace elements. |
| 245 | * The <tt>maxDepth</tt> parameter indicates the maximum number of |
| 246 | * {@link StackTraceElement} to be retrieved from the stack trace. |
| 247 | * If <tt>maxDepth == Integer.MAX_VALUE</tt>, the entire stack trace of |
| 248 | * the thread will be dumped. |
| 249 | * If <tt>maxDepth == 0</tt>, no stack trace of the thread |
| 250 | * will be dumped. |
| 251 | * This method does not obtain the locked monitors and locked |
| 252 | * synchronizers of the thread. |
| 253 | * <p> |
| 254 | * When the Java virtual machine has no stack trace information |
| 255 | * about a thread or <tt>maxDepth == 0</tt>, |
| 256 | * the stack trace in the |
| 257 | * <tt>ThreadInfo</tt> object will be an empty array of |
| 258 | * <tt>StackTraceElement</tt>. |
| 259 | * |
| 260 | * <p> |
| 261 | * If a thread of the given ID is not alive or does not exist, |
| 262 | * this method will return <tt>null</tt>. A thread is alive if |
| 263 | * it has been started and has not yet died. |
| 264 | * |
| 265 | * <p> |
| 266 | * <b>MBeanServer access</b>:<br> |
| 267 | * The mapped type of <tt>ThreadInfo</tt> is |
| 268 | * <tt>CompositeData</tt> with attributes as specified in the |
| 269 | * {@link ThreadInfo#from ThreadInfo.from} method. |
| 270 | * |
| 271 | * @param id the thread ID of the thread. Must be positive. |
| 272 | * @param maxDepth the maximum number of entries in the stack trace |
| 273 | * to be dumped. <tt>Integer.MAX_VALUE</tt> could be used to request |
| 274 | * the entire stack to be dumped. |
| 275 | * |
| 276 | * @return a {@link ThreadInfo} of the thread of the given ID |
| 277 | * with no locked monitor and synchronizer info. |
| 278 | * <tt>null</tt> if the thread of the given ID is not alive or |
| 279 | * it does not exist. |
| 280 | * |
| 281 | * @throws IllegalArgumentException if <tt>id <= 0</tt>. |
| 282 | * @throws IllegalArgumentException if <tt>maxDepth is negative</tt>. |
| 283 | * @throws java.lang.SecurityException if a security manager |
| 284 | * exists and the caller does not have |
| 285 | * ManagementPermission("monitor"). |
| 286 | * |
| 287 | */ |
| 288 | public ThreadInfo getThreadInfo(long id, int maxDepth); |
| 289 | |
| 290 | /** |
| 291 | * Returns the thread info for each thread |
| 292 | * whose ID is in the input array <tt>ids</tt>, |
| 293 | * with stack trace of a specified number of stack trace elements. |
| 294 | * The <tt>maxDepth</tt> parameter indicates the maximum number of |
| 295 | * {@link StackTraceElement} to be retrieved from the stack trace. |
| 296 | * If <tt>maxDepth == Integer.MAX_VALUE</tt>, the entire stack trace of |
| 297 | * the thread will be dumped. |
| 298 | * If <tt>maxDepth == 0</tt>, no stack trace of the thread |
| 299 | * will be dumped. |
| 300 | * This method does not obtain the locked monitors and locked |
| 301 | * synchronizers of the threads. |
| 302 | * <p> |
| 303 | * When the Java virtual machine has no stack trace information |
| 304 | * about a thread or <tt>maxDepth == 0</tt>, |
| 305 | * the stack trace in the |
| 306 | * <tt>ThreadInfo</tt> object will be an empty array of |
| 307 | * <tt>StackTraceElement</tt>. |
| 308 | * <p> |
| 309 | * This method returns an array of the <tt>ThreadInfo</tt> objects, |
| 310 | * each is the thread information about the thread with the same index |
| 311 | * as in the <tt>ids</tt> array. |
| 312 | * If a thread of the given ID is not alive or does not exist, |
| 313 | * <tt>null</tt> will be set in the corresponding element |
| 314 | * in the returned array. A thread is alive if |
| 315 | * it has been started and has not yet died. |
| 316 | * |
| 317 | * <p> |
| 318 | * <b>MBeanServer access</b>:<br> |
| 319 | * The mapped type of <tt>ThreadInfo</tt> is |
| 320 | * <tt>CompositeData</tt> with attributes as specified in the |
| 321 | * {@link ThreadInfo#from ThreadInfo.from} method. |
| 322 | * |
| 323 | * @param ids an array of thread IDs |
| 324 | * @param maxDepth the maximum number of entries in the stack trace |
| 325 | * to be dumped. <tt>Integer.MAX_VALUE</tt> could be used to request |
| 326 | * the entire stack to be dumped. |
| 327 | * |
| 328 | * @return an array of the {@link ThreadInfo} objects, each containing |
| 329 | * information about a thread whose ID is in the corresponding |
| 330 | * element of the input array of IDs with no locked monitor and |
| 331 | * synchronizer info. |
| 332 | * |
| 333 | * @throws IllegalArgumentException if <tt>maxDepth is negative</tt>. |
| 334 | * @throws IllegalArgumentException if any element in the input array |
| 335 | * <tt>ids</tt> is <tt><= 0</tt>. |
| 336 | * @throws java.lang.SecurityException if a security manager |
| 337 | * exists and the caller does not have |
| 338 | * ManagementPermission("monitor"). |
| 339 | * |
| 340 | */ |
| 341 | public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth); |
| 342 | |
| 343 | /** |
| 344 | * Tests if the Java virtual machine supports thread contention monitoring. |
| 345 | * |
| 346 | * @return |
| 347 | * <tt>true</tt> |
| 348 | * if the Java virtual machine supports thread contention monitoring; |
| 349 | * <tt>false</tt> otherwise. |
| 350 | */ |
| 351 | public boolean isThreadContentionMonitoringSupported(); |
| 352 | |
| 353 | /** |
| 354 | * Tests if thread contention monitoring is enabled. |
| 355 | * |
| 356 | * @return <tt>true</tt> if thread contention monitoring is enabled; |
| 357 | * <tt>false</tt> otherwise. |
| 358 | * |
| 359 | * @throws java.lang.UnsupportedOperationException if the Java virtual |
| 360 | * machine does not support thread contention monitoring. |
| 361 | * |
| 362 | * @see #isThreadContentionMonitoringSupported |
| 363 | */ |
| 364 | public boolean isThreadContentionMonitoringEnabled(); |
| 365 | |
| 366 | /** |
| 367 | * Enables or disables thread contention monitoring. |
| 368 | * Thread contention monitoring is disabled by default. |
| 369 | * |
| 370 | * @param enable <tt>true</tt> to enable; |
| 371 | * <tt>false</tt> to disable. |
| 372 | * |
| 373 | * @throws java.lang.UnsupportedOperationException if the Java |
| 374 | * virtual machine does not support thread contention monitoring. |
| 375 | * |
| 376 | * @throws java.lang.SecurityException if a security manager |
| 377 | * exists and the caller does not have |
| 378 | * ManagementPermission("control"). |
| 379 | * |
| 380 | * @see #isThreadContentionMonitoringSupported |
| 381 | */ |
| 382 | public void setThreadContentionMonitoringEnabled(boolean enable); |
| 383 | |
| 384 | /** |
| 385 | * Returns the total CPU time for the current thread in nanoseconds. |
| 386 | * The returned value is of nanoseconds precision but |
| 387 | * not necessarily nanoseconds accuracy. |
| 388 | * If the implementation distinguishes between user mode time and system |
| 389 | * mode time, the returned CPU time is the amount of time that |
| 390 | * the current thread has executed in user mode or system mode. |
| 391 | * |
| 392 | * <p> |
| 393 | * This is a convenient method for local management use and is |
| 394 | * equivalent to calling: |
| 395 | * <blockquote><pre> |
| 396 | * {@link #getThreadCpuTime getThreadCpuTime}(Thread.currentThread().getId()); |
| 397 | * </pre></blockquote> |
| 398 | * |
| 399 | * @return the total CPU time for the current thread if CPU time |
| 400 | * measurement is enabled; <tt>-1</tt> otherwise. |
| 401 | * |
| 402 | * @throws java.lang.UnsupportedOperationException if the Java |
| 403 | * virtual machine does not support CPU time measurement for |
| 404 | * the current thread. |
| 405 | * |
| 406 | * @see #getCurrentThreadUserTime |
| 407 | * @see #isCurrentThreadCpuTimeSupported |
| 408 | * @see #isThreadCpuTimeEnabled |
| 409 | * @see #setThreadCpuTimeEnabled |
| 410 | */ |
| 411 | public long getCurrentThreadCpuTime(); |
| 412 | |
| 413 | /** |
| 414 | * Returns the CPU time that the current thread has executed |
| 415 | * in user mode in nanoseconds. |
| 416 | * The returned value is of nanoseconds precision but |
| 417 | * not necessarily nanoseconds accuracy. |
| 418 | * |
| 419 | * <p> |
| 420 | * This is a convenient method for local management use and is |
| 421 | * equivalent to calling: |
| 422 | * <blockquote><pre> |
| 423 | * {@link #getThreadUserTime getThreadUserTime}(Thread.currentThread().getId()); |
| 424 | * </pre></blockquote> |
| 425 | * |
| 426 | * @return the user-level CPU time for the current thread if CPU time |
| 427 | * measurement is enabled; <tt>-1</tt> otherwise. |
| 428 | * |
| 429 | * @throws java.lang.UnsupportedOperationException if the Java |
| 430 | * virtual machine does not support CPU time measurement for |
| 431 | * the current thread. |
| 432 | * |
| 433 | * @see #getCurrentThreadCpuTime |
| 434 | * @see #isCurrentThreadCpuTimeSupported |
| 435 | * @see #isThreadCpuTimeEnabled |
| 436 | * @see #setThreadCpuTimeEnabled |
| 437 | */ |
| 438 | public long getCurrentThreadUserTime(); |
| 439 | |
| 440 | /** |
| 441 | * Returns the total CPU time for a thread of the specified ID in nanoseconds. |
| 442 | * The returned value is of nanoseconds precision but |
| 443 | * not necessarily nanoseconds accuracy. |
| 444 | * If the implementation distinguishes between user mode time and system |
| 445 | * mode time, the returned CPU time is the amount of time that |
| 446 | * the thread has executed in user mode or system mode. |
| 447 | * |
| 448 | * <p> |
| 449 | * If the thread of the specified ID is not alive or does not exist, |
| 450 | * this method returns <tt>-1</tt>. If CPU time measurement |
| 451 | * is disabled, this method returns <tt>-1</tt>. |
| 452 | * A thread is alive if it has been started and has not yet died. |
| 453 | * <p> |
| 454 | * If CPU time measurement is enabled after the thread has started, |
| 455 | * the Java virtual machine implementation may choose any time up to |
| 456 | * and including the time that the capability is enabled as the point |
| 457 | * where CPU time measurement starts. |
| 458 | * |
| 459 | * @param id the thread ID of a thread |
| 460 | * @return the total CPU time for a thread of the specified ID |
| 461 | * if the thread of the specified ID exists, the thread is alive, |
| 462 | * and CPU time measurement is enabled; |
| 463 | * <tt>-1</tt> otherwise. |
| 464 | * |
| 465 | * @throws IllegalArgumentException if <tt>id <= 0 </tt>. |
| 466 | * @throws java.lang.UnsupportedOperationException if the Java |
| 467 | * virtual machine does not support CPU time measurement for |
| 468 | * other threads. |
| 469 | * |
| 470 | * @see #getThreadUserTime |
| 471 | * @see #isThreadCpuTimeSupported |
| 472 | * @see #isThreadCpuTimeEnabled |
| 473 | * @see #setThreadCpuTimeEnabled |
| 474 | */ |
| 475 | public long getThreadCpuTime(long id); |
| 476 | |
| 477 | /** |
| 478 | * Returns the CPU time that a thread of the specified ID |
| 479 | * has executed in user mode in nanoseconds. |
| 480 | * The returned value is of nanoseconds precision but |
| 481 | * not necessarily nanoseconds accuracy. |
| 482 | * |
| 483 | * <p> |
| 484 | * If the thread of the specified ID is not alive or does not exist, |
| 485 | * this method returns <tt>-1</tt>. If CPU time measurement |
| 486 | * is disabled, this method returns <tt>-1</tt>. |
| 487 | * A thread is alive if it has been started and has not yet died. |
| 488 | * <p> |
| 489 | * If CPU time measurement is enabled after the thread has started, |
| 490 | * the Java virtual machine implementation may choose any time up to |
| 491 | * and including the time that the capability is enabled as the point |
| 492 | * where CPU time measurement starts. |
| 493 | * |
| 494 | * @param id the thread ID of a thread |
| 495 | * @return the user-level CPU time for a thread of the specified ID |
| 496 | * if the thread of the specified ID exists, the thread is alive, |
| 497 | * and CPU time measurement is enabled; |
| 498 | * <tt>-1</tt> otherwise. |
| 499 | * |
| 500 | * @throws IllegalArgumentException if <tt>id <= 0 </tt>. |
| 501 | * @throws java.lang.UnsupportedOperationException if the Java |
| 502 | * virtual machine does not support CPU time measurement for |
| 503 | * other threads. |
| 504 | * |
| 505 | * @see #getThreadCpuTime |
| 506 | * @see #isThreadCpuTimeSupported |
| 507 | * @see #isThreadCpuTimeEnabled |
| 508 | * @see #setThreadCpuTimeEnabled |
| 509 | */ |
| 510 | public long getThreadUserTime(long id); |
| 511 | |
| 512 | /** |
| 513 | * Tests if the Java virtual machine implementation supports CPU time |
| 514 | * measurement for any thread. |
| 515 | * A Java virtual machine implementation that supports CPU time |
| 516 | * measurement for any thread will also support CPU time |
| 517 | * measurement for the current thread. |
| 518 | * |
| 519 | * @return |
| 520 | * <tt>true</tt> |
| 521 | * if the Java virtual machine supports CPU time |
| 522 | * measurement for any thread; |
| 523 | * <tt>false</tt> otherwise. |
| 524 | */ |
| 525 | public boolean isThreadCpuTimeSupported(); |
| 526 | |
| 527 | /** |
| 528 | * Tests if the Java virtual machine supports CPU time |
| 529 | * measurement for the current thread. |
| 530 | * This method returns <tt>true</tt> if {@link #isThreadCpuTimeSupported} |
| 531 | * returns <tt>true</tt>. |
| 532 | * |
| 533 | * @return |
| 534 | * <tt>true</tt> |
| 535 | * if the Java virtual machine supports CPU time |
| 536 | * measurement for current thread; |
| 537 | * <tt>false</tt> otherwise. |
| 538 | */ |
| 539 | public boolean isCurrentThreadCpuTimeSupported(); |
| 540 | |
| 541 | /** |
| 542 | * Tests if thread CPU time measurement is enabled. |
| 543 | * |
| 544 | * @return <tt>true</tt> if thread CPU time measurement is enabled; |
| 545 | * <tt>false</tt> otherwise. |
| 546 | * |
| 547 | * @throws java.lang.UnsupportedOperationException if the Java virtual |
| 548 | * machine does not support CPU time measurement for other threads |
| 549 | * nor for the current thread. |
| 550 | * |
| 551 | * @see #isThreadCpuTimeSupported |
| 552 | * @see #isCurrentThreadCpuTimeSupported |
| 553 | */ |
| 554 | public boolean isThreadCpuTimeEnabled(); |
| 555 | |
| 556 | /** |
| 557 | * Enables or disables thread CPU time measurement. The default |
| 558 | * is platform dependent. |
| 559 | * |
| 560 | * @param enable <tt>true</tt> to enable; |
| 561 | * <tt>false</tt> to disable. |
| 562 | * |
| 563 | * @throws java.lang.UnsupportedOperationException if the Java |
| 564 | * virtual machine does not support CPU time measurement for |
| 565 | * any threads nor for the current thread. |
| 566 | * |
| 567 | * @throws java.lang.SecurityException if a security manager |
| 568 | * exists and the caller does not have |
| 569 | * ManagementPermission("control"). |
| 570 | * |
| 571 | * @see #isThreadCpuTimeSupported |
| 572 | * @see #isCurrentThreadCpuTimeSupported |
| 573 | */ |
| 574 | public void setThreadCpuTimeEnabled(boolean enable); |
| 575 | |
| 576 | /** |
| 577 | * Finds cycles of threads that are in deadlock waiting to acquire |
| 578 | * object monitors. That is, threads that are blocked waiting to enter a |
| 579 | * synchronization block or waiting to reenter a synchronization block |
| 580 | * after an {@link Object#wait Object.wait} call, |
| 581 | * where each thread owns one monitor while |
| 582 | * trying to obtain another monitor already held by another thread |
| 583 | * in a cycle. |
| 584 | * <p> |
| 585 | * More formally, a thread is <em>monitor deadlocked</em> if it is |
| 586 | * part of a cycle in the relation "is waiting for an object monitor |
| 587 | * owned by". In the simplest case, thread A is blocked waiting |
| 588 | * for a monitor owned by thread B, and thread B is blocked waiting |
| 589 | * for a monitor owned by thread A. |
| 590 | * <p> |
| 591 | * This method is designed for troubleshooting use, but not for |
| 592 | * synchronization control. It might be an expensive operation. |
| 593 | * <p> |
| 594 | * This method finds deadlocks involving only object monitors. |
| 595 | * To find deadlocks involving both object monitors and |
| 596 | * <a href="LockInfo.html#OwnableSynchronizer">ownable synchronizers</a>, |
| 597 | * the {@link #findDeadlockedThreads findDeadlockedThreads} method |
| 598 | * should be used. |
| 599 | * |
| 600 | * @return an array of IDs of the threads that are monitor |
| 601 | * deadlocked, if any; <tt>null</tt> otherwise. |
| 602 | * |
| 603 | * @throws java.lang.SecurityException if a security manager |
| 604 | * exists and the caller does not have |
| 605 | * ManagementPermission("monitor"). |
| 606 | * |
| 607 | * @see #findDeadlockedThreads |
| 608 | */ |
| 609 | public long[] findMonitorDeadlockedThreads(); |
| 610 | |
| 611 | /** |
| 612 | * Resets the peak thread count to the current number of |
| 613 | * live threads. |
| 614 | * |
| 615 | * @throws java.lang.SecurityException if a security manager |
| 616 | * exists and the caller does not have |
| 617 | * ManagementPermission("control"). |
| 618 | * |
| 619 | * @see #getPeakThreadCount |
| 620 | * @see #getThreadCount |
| 621 | */ |
| 622 | public void resetPeakThreadCount(); |
| 623 | |
| 624 | /** |
| 625 | * Finds cycles of threads that are in deadlock waiting to acquire |
| 626 | * object monitors or |
| 627 | * <a href="LockInfo.html#OwnableSynchronizer">ownable synchronizers</a>. |
| 628 | * |
| 629 | * Threads are <em>deadlocked</em> in a cycle waiting for a lock of |
| 630 | * these two types if each thread owns one lock while |
| 631 | * trying to acquire another lock already held |
| 632 | * by another thread in the cycle. |
| 633 | * <p> |
| 634 | * This method is designed for troubleshooting use, but not for |
| 635 | * synchronization control. It might be an expensive operation. |
| 636 | * |
| 637 | * @return an array of IDs of the threads that are |
| 638 | * deadlocked waiting for object monitors or ownable synchronizers, if any; |
| 639 | * <tt>null</tt> otherwise. |
| 640 | * |
| 641 | * @throws java.lang.SecurityException if a security manager |
| 642 | * exists and the caller does not have |
| 643 | * ManagementPermission("monitor"). |
| 644 | * @throws java.lang.UnsupportedOperationException if the Java virtual |
| 645 | * machine does not support monitoriing of ownable synchronizer usage. |
| 646 | * |
| 647 | * @see #isSynchronizerUsageSupported |
| 648 | * @see #findMonitorDeadlockedThreads |
| 649 | * @since 1.6 |
| 650 | */ |
| 651 | public long[] findDeadlockedThreads(); |
| 652 | |
| 653 | /** |
| 654 | * Tests if the Java virtual machine supports monitoring of |
| 655 | * object monitor usage. |
| 656 | * |
| 657 | * @return |
| 658 | * <tt>true</tt> |
| 659 | * if the Java virtual machine supports monitoring of |
| 660 | * object monitor usage; |
| 661 | * <tt>false</tt> otherwise. |
| 662 | * |
| 663 | * @see #dumpAllThreads |
| 664 | * @since 1.6 |
| 665 | */ |
| 666 | public boolean isObjectMonitorUsageSupported(); |
| 667 | |
| 668 | /** |
| 669 | * Tests if the Java virtual machine supports monitoring of |
| 670 | * <a href="LockInfo.html#OwnableSynchronizer"> |
| 671 | * ownable synchronizer</a> usage. |
| 672 | * |
| 673 | * @return |
| 674 | * <tt>true</tt> |
| 675 | * if the Java virtual machine supports monitoring of ownable |
| 676 | * synchronizer usage; |
| 677 | * <tt>false</tt> otherwise. |
| 678 | * |
| 679 | * @see #dumpAllThreads |
| 680 | * @since 1.6 |
| 681 | */ |
| 682 | public boolean isSynchronizerUsageSupported(); |
| 683 | |
| 684 | /** |
| 685 | * Returns the thread info for each thread |
| 686 | * whose ID is in the input array <tt>ids</tt>, with stack trace |
| 687 | * and synchronization information. |
| 688 | * |
| 689 | * <p> |
| 690 | * This method obtains a snapshot of the thread information |
| 691 | * for each thread including: |
| 692 | * <ul> |
| 693 | * <li>the entire stack trace,</li> |
| 694 | * <li>the object monitors currently locked by the thread |
| 695 | * if <tt>lockedMonitors</tt> is <tt>true</tt>, and</li> |
| 696 | * <li>the <a href="LockInfo.html#OwnableSynchronizer"> |
| 697 | * ownable synchronizers</a> currently locked by the thread |
| 698 | * if <tt>lockedSynchronizers</tt> is <tt>true</tt>.</li> |
| 699 | * </ul> |
| 700 | * <p> |
| 701 | * This method returns an array of the <tt>ThreadInfo</tt> objects, |
| 702 | * each is the thread information about the thread with the same index |
| 703 | * as in the <tt>ids</tt> array. |
| 704 | * If a thread of the given ID is not alive or does not exist, |
| 705 | * <tt>null</tt> will be set in the corresponding element |
| 706 | * in the returned array. A thread is alive if |
| 707 | * it has been started and has not yet died. |
| 708 | * <p> |
| 709 | * If a thread does not lock any object monitor or <tt>lockedMonitors</tt> |
| 710 | * is <tt>false</tt>, the returned <tt>ThreadInfo</tt> object will have an |
| 711 | * empty <tt>MonitorInfo</tt> array. Similarly, if a thread does not |
| 712 | * lock any synchronizer or <tt>lockedSynchronizers</tt> is <tt>false</tt>, |
| 713 | * the returned <tt>ThreadInfo</tt> object |
| 714 | * will have an empty <tt>LockInfo</tt> array. |
| 715 | * |
| 716 | * <p> |
| 717 | * When both <tt>lockedMonitors</tt> and <tt>lockedSynchronizers</tt> |
| 718 | * parameters are <tt>false</tt>, it is equivalent to calling: |
| 719 | * <blockquote><pre> |
| 720 | * {@link #getThreadInfo(long[], int) getThreadInfo(ids, Integer.MAX_VALUE)} |
| 721 | * </pre></blockquote> |
| 722 | * |
| 723 | * <p> |
| 724 | * This method is designed for troubleshooting use, but not for |
| 725 | * synchronization control. It might be an expensive operation. |
| 726 | * |
| 727 | * <p> |
| 728 | * <b>MBeanServer access</b>:<br> |
| 729 | * The mapped type of <tt>ThreadInfo</tt> is |
| 730 | * <tt>CompositeData</tt> with attributes as specified in the |
| 731 | * {@link ThreadInfo#from ThreadInfo.from} method. |
| 732 | * |
| 733 | * @param ids an array of thread IDs. |
| 734 | * @param lockedMonitors if <tt>true</tt>, retrieves all locked monitors. |
| 735 | * @param lockedSynchronizers if <tt>true</tt>, retrieves all locked |
| 736 | * ownable synchronizers. |
| 737 | * |
| 738 | * @return an array of the {@link ThreadInfo} objects, each containing |
| 739 | * information about a thread whose ID is in the corresponding |
| 740 | * element of the input array of IDs. |
| 741 | * |
| 742 | * @throws java.lang.SecurityException if a security manager |
| 743 | * exists and the caller does not have |
| 744 | * ManagementPermission("monitor"). |
| 745 | * @throws java.lang.UnsupportedOperationException |
| 746 | * <ul> |
| 747 | * <li>if <tt>lockedMonitors</tt> is <tt>true</tt> but |
| 748 | * the Java virtual machine does not support monitoring |
| 749 | * of {@linkplain #isObjectMonitorUsageSupported |
| 750 | * object monitor usage}; or</li> |
| 751 | * <li>if <tt>lockedSynchronizers</tt> is <tt>true</tt> but |
| 752 | * the Java virtual machine does not support monitoring |
| 753 | * of {@linkplain #isSynchronizerUsageSupported |
| 754 | * ownable synchronizer usage}.</li> |
| 755 | * </ul> |
| 756 | * |
| 757 | * @see #isObjectMonitorUsageSupported |
| 758 | * @see #isSynchronizerUsageSupported |
| 759 | * |
| 760 | * @since 1.6 |
| 761 | */ |
| 762 | public ThreadInfo[] getThreadInfo(long[] ids, boolean lockedMonitors, boolean lockedSynchronizers); |
| 763 | |
| 764 | /** |
| 765 | * Returns the thread info for all live threads with stack trace |
| 766 | * and synchronization information. |
| 767 | * Some threads included in the returned array |
| 768 | * may have been terminated when this method returns. |
| 769 | * |
| 770 | * <p> |
| 771 | * This method returns an array of {@link ThreadInfo} objects |
| 772 | * as specified in the {@link #getThreadInfo(long[], boolean, boolean)} |
| 773 | * method. |
| 774 | * |
| 775 | * @param lockedMonitors if <tt>true</tt>, dump all locked monitors. |
| 776 | * @param lockedSynchronizers if <tt>true</tt>, dump all locked |
| 777 | * ownable synchronizers. |
| 778 | * |
| 779 | * @return an array of {@link ThreadInfo} for all live threads. |
| 780 | * |
| 781 | * @throws java.lang.SecurityException if a security manager |
| 782 | * exists and the caller does not have |
| 783 | * ManagementPermission("monitor"). |
| 784 | * @throws java.lang.UnsupportedOperationException |
| 785 | * <ul> |
| 786 | * <li>if <tt>lockedMonitors</tt> is <tt>true</tt> but |
| 787 | * the Java virtual machine does not support monitoring |
| 788 | * of {@linkplain #isObjectMonitorUsageSupported |
| 789 | * object monitor usage}; or</li> |
| 790 | * <li>if <tt>lockedSynchronizers</tt> is <tt>true</tt> but |
| 791 | * the Java virtual machine does not support monitoring |
| 792 | * of {@linkplain #isSynchronizerUsageSupported |
| 793 | * ownable synchronizer usage}.</li> |
| 794 | * </ul> |
| 795 | * |
| 796 | * @see #isObjectMonitorUsageSupported |
| 797 | * @see #isSynchronizerUsageSupported |
| 798 | * |
| 799 | * @since 1.6 |
| 800 | */ |
| 801 | public ThreadInfo[] dumpAllThreads(boolean lockedMonitors, boolean lockedSynchronizers); |
| 802 | } |