bb.util
Class ThreadMeasurer

java.lang.Object
  extended by bb.util.ThreadMeasurer
Direct Known Subclasses:
ThreadMonitor.UnitTest.ThreadMeasurerTest

public class ThreadMeasurer
extends Object

Class which measures the threads of a JVM.

This class was adapted from the class <JDK_HOME>/demo/management/FullThreadDump/src/ThreadMonitor.java written by Mandy Chung.

This class is multithread safe: the public static methods do not use any state of this class, and the public instance methods are all synchronized. The protected instance methods are not synchronized, and subclass authors should ensure that they are only called from public synchronized methods.

Author:
Brent Boyer

Nested Class Summary
static class ThreadMeasurer.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Field Summary
private  boolean reportThreadContention
           
private  boolean reportThreadTimes
           
private  ThreadMXBean threadMXBean
          Contract: is never null.
 
Constructor Summary
ThreadMeasurer()
          Constructs an instance which gets thread information from the local JVM.
ThreadMeasurer(MBeanServerConnection server)
          Constructs an instance which gets thread information from the JVM specified by server.
ThreadMeasurer(ThreadMXBean threadMXBean)
          Constructs an instance which gets thread information from threadMXBean.
 
Method Summary
protected  void appendThreadInfo(ThreadInfo threadInfo, StringBuilder sb)
          Appends all of the data in threadInfo to sb.
protected  void appendThreadInfoArray(ThreadInfo[] threadInfos, StringBuilder sb)
          Appends the data in threadInfos to sb.
static boolean attemptCpuTimeMeasurement(ThreadMXBean threadMXBean)
          Trys to ensure that threadMXBean has enabled thread cpu time measurement.
static boolean attemptThreadContentionMonitoring(ThreadMXBean threadMXBean)
          Trys to ensure that threadMXBean has enabled thread contention monitoring (i.e. for collecting per-thread statistics about monitor contention).
 String getDeadlockState()
          Checks if any threads are deadlocked.
static ThreadMXBean getThreadMXBean(MBeanServerConnection server)
          Returns a ThreadMXBean instance for the JVM specified by server.
 String getThreadState()
          Returns a String description of the thread state of the monitored JVM.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

threadMXBean

private final ThreadMXBean threadMXBean
Contract: is never null.


reportThreadTimes

private final boolean reportThreadTimes

reportThreadContention

private final boolean reportThreadContention
Constructor Detail

ThreadMeasurer

public ThreadMeasurer()
Constructs an instance which gets thread information from the local JVM.


ThreadMeasurer

public ThreadMeasurer(MBeanServerConnection server)
               throws IllegalArgumentException,
                      IOException
Constructs an instance which gets thread information from the JVM specified by server. Typically, server refers to a remote JVM.

Throws:
IllegalArgumentException - if server == null
IOException - if a communication problem occurrs when accessing server

ThreadMeasurer

public ThreadMeasurer(ThreadMXBean threadMXBean)
               throws IllegalArgumentException
Constructs an instance which gets thread information from threadMXBean.

Throws:
IllegalArgumentException - if threadMXBean == null
Method Detail

getThreadMXBean

public static ThreadMXBean getThreadMXBean(MBeanServerConnection server)
                                    throws IllegalArgumentException,
                                           IOException
Returns a ThreadMXBean instance for the JVM specified by server.

Throws:
IllegalArgumentException - if server == null
IOException - if a communication problem occurrs when accessing server

attemptCpuTimeMeasurement

public static boolean attemptCpuTimeMeasurement(ThreadMXBean threadMXBean)
                                         throws IllegalArgumentException
Trys to ensure that threadMXBean has enabled thread cpu time measurement. (i.e. for collecting per-thread statistics about cpu usage). If this measurement is already enabled, this method does nothing. Else if the JVM supports it but it was disabled when this method was called, then it will be enabled by this method. Otherwise, if the JVM does not supports it, this method does nothing.

Returns:
true if upon return thread cpu time measurement is enabled, false otherwise
Throws:
IllegalArgumentException - if threadMXBean == null

attemptThreadContentionMonitoring

public static boolean attemptThreadContentionMonitoring(ThreadMXBean threadMXBean)
                                                 throws IllegalArgumentException
Trys to ensure that threadMXBean has enabled thread contention monitoring (i.e. for collecting per-thread statistics about monitor contention). If this monitoring is already enabled, this method does nothing. Else if the JVM supports it but it was disabled when this method was called, then it will be enabled by this method. Otherwise, if the JVM does not supports it, this method does nothing.

Returns:
true if upon return thread contention monitoring is enabled, false otherwise
Throws:
IllegalArgumentException - if threadMXBean == null

getThreadState

public String getThreadState()
                      throws UnsupportedOperationException
Returns a String description of the thread state of the monitored JVM.

Contract: the result is never null.

Throws:
UnsupportedOperationException - if reportThreadTimes was set to true in the constructor but some other class called threadMXBean.setThreadCpuTimeEnabled(false).

getDeadlockState

public String getDeadlockState()
                        throws SecurityException
Checks if any threads are deadlocked. If any are, returns a String that fully describes the state of the deadlocked threads. Otherwise, returns null.

This method will attempt to find deadlocks in both monitors as well as ownable synchronizers if possible. But if the platform does not support finding deadlock detection in ownable synchronizers, it falls back to examining just monitors.

Throws:
SecurityException - if a security manager exists and the caller does not have ManagementPermission("monitor")

appendThreadInfoArray

protected void appendThreadInfoArray(ThreadInfo[] threadInfos,
                                     StringBuilder sb)
                              throws UnsupportedOperationException
Appends the data in threadInfos to sb.

Skips over any element that is null. This behavior is critical, because if the threadInfos arg came as a result of calling ThreadMXBean.getThreadInfo, then null elements may be present for threads which died by the time that getThreadInfo was called.

Throws:
UnsupportedOperationException - if reportThreadTimes was set to true in the constructor but some other class called threadMXBean.setThreadCpuTimeEnabled(false).

appendThreadInfo

protected void appendThreadInfo(ThreadInfo threadInfo,
                                StringBuilder sb)
                         throws UnsupportedOperationException
Appends all of the data in threadInfo to sb.

Throws:
UnsupportedOperationException - if reportThreadTimes was set to true in the constructor but some other class called threadMXBean.setThreadCpuTimeEnabled(false).