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> 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 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); } } }