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

32 lines
963 B
Java
Raw Permalink Normal View History

2023-01-07 16:48:53 +00:00
package ex9;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public final class Profiler {
2023-01-09 18:15:25 +00:00
private static final ConcurrentMap<Thread, List<String>> callLog = new ConcurrentHashMap<>();
2023-01-07 16:48:53 +00:00
static {
Runtime.getRuntime().addShutdownHook(new Thread(Profiler::print));
}
private Profiler() {
}
public static void logCall(final Thread thread, final String methodName) {
2023-01-09 18:15:25 +00:00
final List<String> list = callLog.computeIfAbsent(thread, ignored -> new LinkedList<>());
2023-01-07 16:48:53 +00:00
// thread safe, as once a thread accesses its own list access to said list is non-concurrent
list.add(methodName);
}
private static void print() {
2023-01-09 18:15:25 +00:00
for (final Thread thread : callLog.keySet()) {
System.out.printf("=== %s ===\n", thread.getName());
callLog.get(thread).forEach(System.out::println);
2023-01-07 16:48:53 +00:00
}
}
}