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

30 lines
858 B
Java
Raw Normal View History

2023-01-09 07:01:15 +00:00
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());
exceptionNameToCount.get(exceptionName).increment();
}
}