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

28 lines
809 B
Java

package ex4;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.LongAdder;
public final class Profiler {
private static final Map<String, LongAdder> exceptionNameToCount = new ConcurrentHashMap<>();
static {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
for (final var entry : exceptionNameToCount.entrySet()) {
System.out.printf("Exception: %s - occurrences: %d%n", entry.getKey(), entry.getValue().intValue());
}
}));
}
private Profiler() {
}
public static void countException(Throwable e) {
final String exceptionName = e.getClass().getName();
exceptionNameToCount.computeIfAbsent(exceptionName, k -> new LongAdder()).increment();
}
}