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

34 lines
984 B
Java
Raw Permalink Normal View History

2023-01-09 07:01:15 +00:00
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(),
2023-01-09 07:01:15 +00:00
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();
2023-01-09 07:01:15 +00:00
}
}