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/ex7/Profiler.java

33 lines
984 B
Java

package ex7;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.LongAdder;
public final class Profiler {
private static final Map<MonitorEntry, LongAdder> monitorToAccessCount = new ConcurrentHashMap<>();
static {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
for (final var entry : monitorToAccessCount.entrySet()) {
System.out.printf("%d - %s - #Locks: %d%n",
entry.getKey().monitorHashCode(),
entry.getKey().name(),
entry.getValue().intValue()
);
}
}));
}
private Profiler() {
}
public static void countMonitorAccess(final Object obj) {
final MonitorEntry monitorEntry = new MonitorEntry(obj.hashCode(), obj.getClass().getName());
monitorToAccessCount.computeIfAbsent(monitorEntry, k -> new LongAdder()).increment();
}
}