All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dropwizard.metrics5.jvm.CachedThreadStatesGaugeSet Maven / Gradle / Ivy

package io.dropwizard.metrics5.jvm;

import io.dropwizard.metrics5.CachedGauge;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.concurrent.TimeUnit;

/**
 * A variation of ThreadStatesGaugeSet that caches the ThreadInfo[] objects for
 * a given interval.
 */
public class CachedThreadStatesGaugeSet extends ThreadStatesGaugeSet {

    private final CachedGauge threadInfo;

    /**
     * Creates a new set of gauges using the given MXBean and detector.
     * Caches the information for the given interval and time unit.
     *
     * @param threadMXBean     a thread MXBean
     * @param deadlockDetector a deadlock detector
     * @param interval         cache interval
     * @param unit             cache interval time unit
     */
    public CachedThreadStatesGaugeSet(final ThreadMXBean threadMXBean, ThreadDeadlockDetector deadlockDetector,
                                      long interval, TimeUnit unit) {
        super(threadMXBean, deadlockDetector);
        threadInfo = new CachedGauge(interval, unit) {
            @Override
            protected ThreadInfo[] loadValue() {
                return CachedThreadStatesGaugeSet.super.getThreadInfo();
            }
        };
    }

    /**
     * Creates a new set of gauges using the default MXBeans.
     * Caches the information for the given interval and time unit.
     *
     * @param interval cache interval
     * @param unit     cache interval time unit
     */
    public CachedThreadStatesGaugeSet(long interval, TimeUnit unit) {
        this(ManagementFactory.getThreadMXBean(), new ThreadDeadlockDetector(), interval, unit);
    }

    @Override
    ThreadInfo[] getThreadInfo() {
        return threadInfo.getValue();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy