JVM Integration for Metrics - A set of classes which allow you to monitor critical aspects of your Java Virtual Machine using Metrics
—
Real-time thread state tracking, deadlock detection, and thread dump generation with optional performance optimization through caching.
Monitors thread states and provides deadlock detection capabilities.
/**
* A set of gauges for the number of threads in their various states and deadlock detection.
*/
public class ThreadStatesGaugeSet implements MetricSet {
/**
* Creates a new set of gauges using the default MXBeans.
*/
public ThreadStatesGaugeSet();
/**
* Creates a new set of gauges using the given MXBean and detector.
* @param threads a thread MXBean
* @param deadlockDetector a deadlock detector
*/
public ThreadStatesGaugeSet(ThreadMXBean threads, ThreadDeadlockDetector deadlockDetector);
/**
* Returns all thread-related metrics.
* @return map of metric names to Metric instances
*/
public Map<String, Metric> getMetrics();
}Performance-optimized version that caches ThreadInfo objects for a specified interval.
/**
* A variation of ThreadStatesGaugeSet that caches the ThreadInfo[] objects for a given interval.
*/
public class CachedThreadStatesGaugeSet extends ThreadStatesGaugeSet {
/**
* Creates a new set of gauges using the default MXBeans with caching.
* @param interval cache interval
* @param unit cache interval time unit
*/
public CachedThreadStatesGaugeSet(long interval, TimeUnit unit);
/**
* Creates a new set of gauges using the given MXBean and detector with caching.
* @param threadMXBean a thread MXBean
* @param deadlockDetector a deadlock detector
* @param interval cache interval
* @param unit cache interval time unit
*/
public CachedThreadStatesGaugeSet(ThreadMXBean threadMXBean, ThreadDeadlockDetector deadlockDetector, long interval, TimeUnit unit);
}Utility class for detecting and reporting thread deadlocks.
/**
* A utility class for detecting deadlocked threads.
*/
public class ThreadDeadlockDetector {
/**
* Creates a new detector using the default ThreadMXBean.
*/
public ThreadDeadlockDetector();
/**
* Creates a new detector using the given ThreadMXBean.
* @param threads a ThreadMXBean
*/
public ThreadDeadlockDetector(ThreadMXBean threads);
/**
* Returns a set of diagnostic stack traces for any deadlocked threads.
* @return stack traces for deadlocked threads or an empty set
*/
public Set<String> getDeadlockedThreads();
}Utility class for generating comprehensive thread dumps.
/**
* A convenience class for getting a thread dump.
*/
public class ThreadDump {
/**
* Creates a new ThreadDump instance.
* @param threadMXBean the ThreadMXBean to use
*/
public ThreadDump(ThreadMXBean threadMXBean);
/**
* Dumps all threads' current information, including synchronization, to an output stream.
* @param out an output stream
*/
public void dump(OutputStream out);
/**
* Dumps all threads' current information with control over synchronization info.
* @param lockedMonitors dump all locked monitors if true
* @param lockedSynchronizers dump all locked ownable synchronizers if true
* @param out an output stream
*/
public void dump(boolean lockedMonitors, boolean lockedSynchronizers, OutputStream out);
}Metrics Provided by ThreadStatesGaugeSet:
new.count, runnable.count, blocked.count, waiting.count, timed_waiting.count, terminated.countcount (total threads), daemon.count, peak.count, total_started.countdeadlock.count (number of deadlocked threads), deadlocks (detailed deadlock information)Usage Examples:
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.jvm.*;
import java.util.concurrent.TimeUnit;
// Basic thread monitoring
MetricRegistry registry = new MetricRegistry();
registry.registerAll(new ThreadStatesGaugeSet());
// Access thread metrics
Gauge<Integer> totalThreads = registry.getGauges().get("count");
Gauge<Integer> runnableThreads = registry.getGauges().get("runnable.count");
Gauge<Integer> deadlockCount = registry.getGauges().get("deadlock.count");
System.out.println("Total threads: " + totalThreads.getValue());
System.out.println("Runnable threads: " + runnableThreads.getValue());
System.out.println("Deadlocked threads: " + deadlockCount.getValue());
// Cached thread monitoring for better performance
registry.registerAll(new CachedThreadStatesGaugeSet(5, TimeUnit.SECONDS));
// Custom thread monitoring with specific beans
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
ThreadDeadlockDetector detector = new ThreadDeadlockDetector(threadBean);
registry.registerAll(new ThreadStatesGaugeSet(threadBean, detector));
// Direct deadlock detection
ThreadDeadlockDetector deadlockDetector = new ThreadDeadlockDetector();
Set<String> deadlocks = deadlockDetector.getDeadlockedThreads();
if (!deadlocks.isEmpty()) {
System.out.println("Deadlocks detected:");
deadlocks.forEach(System.out::println);
}
// Generate thread dump
ThreadDump threadDump = new ThreadDump(ManagementFactory.getThreadMXBean());
threadDump.dump(System.out); // Full dump with synchronization info
threadDump.dump(false, false, System.out); // Basic dump without sync infoPerformance Considerations:
CachedThreadStatesGaugeSet in high-frequency monitoring scenarios to reduce overheadInstall with Tessl CLI
npx tessl i tessl/maven-io-dropwizard-metrics--metrics-jvm