From 74054345190d5a660af87332e6a69c64480547b2 Mon Sep 17 00:00:00 2001 From: granny Date: Sun, 21 Jul 2024 23:12:52 -0700 Subject: [PATCH] move settings file I/O into it's own thread, closes #38 --- .../renderer/task/UpdateSettingsData.java | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/net/pl3x/map/core/renderer/task/UpdateSettingsData.java b/core/src/main/java/net/pl3x/map/core/renderer/task/UpdateSettingsData.java index 76e4a42a7..7e3e247ac 100644 --- a/core/src/main/java/net/pl3x/map/core/renderer/task/UpdateSettingsData.java +++ b/core/src/main/java/net/pl3x/map/core/renderer/task/UpdateSettingsData.java @@ -30,6 +30,8 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; import net.pl3x.map.core.Pl3xMap; import net.pl3x.map.core.configuration.Config; import net.pl3x.map.core.configuration.Lang; @@ -51,18 +53,38 @@ public class UpdateSettingsData extends Task { .setLenient() .create(); private int jsonHashCache = -1; + private final ExecutorService executor; + + private CompletableFuture future; + private boolean running; public UpdateSettingsData() { super(1, true); + this.executor = Pl3xMap.ThreadFactory.createService("Pl3xMap-Settings"); + } + + @Override + public void cancel() { + super.cancel(); + if (this.future != null) { + this.future.cancel(true); + } } @Override public void run() { - try { - parseSettings(); - } catch (Throwable t) { - Logger.severe("Failed to parse settings.json", t); + if (this.running) { + return; } + this.running = true; + this.future = CompletableFuture.runAsync(() -> { + try { + parseSettings(); + } catch (Throwable t) { + Logger.severe("Failed to parse settings.json", t); + } + this.running = false; + }, this.executor); } private @NotNull List<@NotNull Map<@NotNull String, @NotNull Object>> parseWorlds() {