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();
}
});
}
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()));
}
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;
}
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) {
}
}
}
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.");
}
Aggregations