Skip to content

Commit

Permalink
move settings file I/O into it's own thread, closes #38
Browse files Browse the repository at this point in the history
  • Loading branch information
granny committed Jul 22, 2024
1 parent 33c332d commit 7405434
Showing 1 changed file with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,18 +53,38 @@ public class UpdateSettingsData extends Task {
.setLenient()
.create();
private int jsonHashCache = -1;
private final ExecutorService executor;

private CompletableFuture<Void> 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() {
Expand Down

0 comments on commit 7405434

Please sign in to comment.