This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
AJP/DiSLProject2022/src-profiler/ex2/Profiler.java

37 lines
1,002 B
Java
Raw Normal View History

2023-01-02 14:18:34 +00:00
package ex2;
import java.util.HashMap;
import java.util.Map;
2023-01-04 16:23:12 +00:00
import java.util.concurrent.ConcurrentHashMap;
2023-01-02 14:18:34 +00:00
import java.util.concurrent.atomic.LongAdder;
public final class Profiler {
2023-01-04 16:23:12 +00:00
private static final Map<Class<?>, Boolean> isThread = new ConcurrentHashMap<>();
2023-01-02 14:18:34 +00:00
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);
}
}