Search in sources :

Example 21 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project hazelcast by hazelcast.

the class ThreadMetricSet method register.

/**
     * Registers all the metrics in this metric pack.
     *
     * @param metricsRegistry the MetricsRegistry upon which the metrics are registered.
     */
public static void register(MetricsRegistry metricsRegistry) {
    checkNotNull(metricsRegistry, "metricsRegistry");
    ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
    metricsRegistry.register(mxBean, "thread.threadCount", MANDATORY, new LongProbeFunction<ThreadMXBean>() {

        @Override
        public long get(ThreadMXBean threadMXBean) {
            return threadMXBean.getThreadCount();
        }
    });
    metricsRegistry.register(mxBean, "thread.peakThreadCount", MANDATORY, new LongProbeFunction<ThreadMXBean>() {

        @Override
        public long get(ThreadMXBean threadMXBean) {
            return threadMXBean.getPeakThreadCount();
        }
    });
    metricsRegistry.register(mxBean, "thread.daemonThreadCount", MANDATORY, new LongProbeFunction<ThreadMXBean>() {

        @Override
        public long get(ThreadMXBean threadMXBean) {
            return threadMXBean.getDaemonThreadCount();
        }
    });
    metricsRegistry.register(mxBean, "thread.totalStartedThreadCount", MANDATORY, new LongProbeFunction<ThreadMXBean>() {

        @Override
        public long get(ThreadMXBean threadMXBean) {
            return threadMXBean.getTotalStartedThreadCount();
        }
    });
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean)

Example 22 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project gocd by gocd.

the class DaemonThreadStatsCollector method captureStats.

public void captureStats(long threadId) {
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    long threadCpuTime = threadMXBean.getThreadCpuTime(threadId);
    cpuInfoConcurrentHashMap.put(threadId, new Info(threadCpuTime, UUID.randomUUID().toString()));
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean)

Example 23 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project gocd by gocd.

the class DaemonThreadStatsCollector method statsFor.

public Map<String, Object> statsFor(long threadId) {
    if (!cpuInfoConcurrentHashMap.containsKey(threadId)) {
        return null;
    }
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    long end = threadMXBean.getThreadCpuTime(threadId);
    Info info = cpuInfoConcurrentHashMap.get(threadId);
    Long start = info.time;
    HashMap<String, Object> map = new HashMap<>();
    map.put("CPUTime(nanoseconds)", end - start);
    map.put("UUID", info.uuid);
    return map;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 24 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project jdk8u_jdk by JetBrains.

the class TestThread method waitUntilBlockingOnObject.

/**
     * Waits until {@link TestThread} is in the certain {@link State}
     * and blocking on {@code object}.
     *
     * @param state The thread state
     * @param object The object to block on
     */
public void waitUntilBlockingOnObject(Thread.State state, Object object) {
    String want = object == null ? null : object.getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(object));
    ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
    while (isAlive()) {
        ThreadInfo ti = tmx.getThreadInfo(getId());
        if (ti.getThreadState() == state && (want == null || want.equals(ti.getLockName()))) {
            return;
        }
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
        }
    }
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo)

Example 25 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project jdk8u_jdk by JetBrains.

the class InvalidThreadID method main.

public static void main(String[] argv) {
    ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
    int cnt = 0;
    long[] idArr = { 0, -1, -2, (Long.MIN_VALUE + 1), Long.MIN_VALUE };
    if (mbean.isThreadCpuTimeSupported()) {
        for (int i = 0; i < idArr.length; i++) {
            try {
                mbean.getThreadCpuTime(idArr[i]);
                System.out.println("Test failed. IllegalArgumentException" + " expected for ID = " + idArr[i]);
            } catch (IllegalArgumentException iae) {
                cnt++;
            }
        }
        if (cnt != idArr.length) {
            throw new RuntimeException("Unexpected number of " + "IllegalArgumentException = " + cnt + " expected = " + idArr.length);
        }
        // CPU time for a non-existence thread
        long time = mbean.getThreadCpuTime(999999);
        if (time < 0 && time != -1) {
            throw new RuntimeException("Cpu time for thread 999999" + " is invalid = " + time + " expected to be -1.");
        }
    }
    System.out.println("Test passed.");
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean)

Aggregations

ThreadMXBean (java.lang.management.ThreadMXBean)87 ThreadInfo (java.lang.management.ThreadInfo)43 HashMap (java.util.HashMap)8 MemoryMXBean (java.lang.management.MemoryMXBean)7 RuntimeMXBean (java.lang.management.RuntimeMXBean)7 IOException (java.io.IOException)6 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)6 ArrayList (java.util.ArrayList)6 File (java.io.File)5 HashSet (java.util.HashSet)5 Map (java.util.Map)5 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)4 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)4 MemoryUsage (java.lang.management.MemoryUsage)4 Date (java.util.Date)4 MonitorInfo (java.lang.management.MonitorInfo)3 Properties (java.util.Properties)3 Test (org.junit.Test)3 SnmpStatusException (com.sun.jmx.snmp.SnmpStatusException)2 FileOutputStream (java.io.FileOutputStream)2