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