AJP/DiSLProject2022/src-profiler/ex2/Profiler.java
tripparsugo c8432e032b "I am ready to meet my Maker. Whether my Maker is prepared for the
great ordeal of meeting me is another matter."
		-- Winston Churchill
2023-01-04 17:23:12 +01:00

37 lines
1002 B
Java

package ex2;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.LongAdder;
public final class Profiler {
private static final Map<Class<?>, Boolean> isThread = new ConcurrentHashMap<>();
private static final LongAdder reads = new LongAdder();
private static final LongAdder writes = new LongAdder();
static {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.out.printf("Instance field read access: %d\nInstance field write access: %d\n",
reads.longValue(),
writes.longValue());
}));
}
private Profiler() {
}
public static boolean isThread(final Class<?> clazz) {
return isThread.computeIfAbsent(clazz, Thread.class::isAssignableFrom);
}
public static void increment(long readCount, long writeCount) {
reads.add(readCount);
writes.add(writeCount);
}
}