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
975 B
Java
Raw 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 {
private static final ConcurrentMap<String, List<String>> callLog = new ConcurrentHashMap<>();
static {
Runtime.getRuntime().addShutdownHook(new Thread(Profiler::print));
}
private Profiler() {
}
public static void logCall(final Thread thread, final String methodName) {
final List<String> list = callLog.computeIfAbsent(thread.getName(), ignored -> new LinkedList<>());
// thread safe, as once a thread accesses its own list access to said list is non-concurrent
list.add(methodName);
}
private static void print() {
for (final String threadName : callLog.keySet()) {
System.out.printf("=== %s ===\n", threadName);
callLog.get(threadName).forEach(System.out::println);
}
}
}