AJP/DiSLProject2022/src-profiler/ex2/Profiler.java
2023-01-07 17:48:53 +01:00

36 lines
964 B
Java

package ex2;
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);
}
}