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

27 lines
786 B
Java
Raw Normal View History

2023-01-09 07:01:15 +00:00
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().longValue());
2023-01-09 07:01:15 +00:00
}
}));
}
private Profiler() {
}
public static void countLoop(String methodName) {
methodToLoopCount.computeIfAbsent(methodName, k -> new LongAdder());
methodToLoopCount.get(methodName).increment();
}
}