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/ex6/Profiler.java
2023-01-09 08:01:15 +01:00

29 lines
788 B
Java

package ex6;
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> methodToLoopCount = new ConcurrentHashMap<>();
static {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
for (final var entry : methodToLoopCount.entrySet()) {
System.out.printf("Method name: %s - # executed loops: %d%n", entry.getKey(), entry.getValue().intValue());
}
}));
}
private Profiler() {
}
public static void countLoop(String methodName) {
methodToLoopCount.computeIfAbsent(methodName, k -> new LongAdder());
methodToLoopCount.get(methodName).increment();
}
}