fix on ex11

This commit is contained in:
Claudio Maggioni 2023-01-28 18:07:24 +01:00
parent aa826bf10f
commit ff6f86ae2a

View file

@ -7,9 +7,10 @@ import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.LongAdder;
public final class Profiler {
private static final Map<CallInfo, long[]> count = new HashMap<>();
private static final Map<CallInfo, LongAdder> count = new HashMap<>();
private static final Path outFile = Path.of("ex11.csv");
@ -18,11 +19,11 @@ public final class Profiler {
static {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
stop = true;
try (final OutputStream path = Files.newOutputStream(outFile, StandardOpenOption.WRITE)) {
try (final OutputStream path = Files.newOutputStream(outFile)) {
final CallInfoCSVWriter w = new CallInfoCSVWriter(path);
for (final var entry : count.entrySet()) {
System.out.println(entry.getKey().toString() + "\n#invokes " + entry.getValue()[0] + "\n");
w.write(entry.getKey(), entry.getValue()[0]);
System.out.println(entry.getKey().toString() + "\n#invokes " + entry.getValue().longValue() + "\n");
w.write(entry.getKey(), entry.getValue().longValue());
}
System.out.println("Invocations have been written to: " + outFile.toAbsolutePath());
} catch (final IOException e) {
@ -36,7 +37,7 @@ public final class Profiler {
public static void registerCall(final CallInfo info) {
if (stop) return;
count.computeIfAbsent(info, ignored -> new long[1])[0]++;
count.computeIfAbsent(info, ignored -> new LongAdder()).increment();
}
}