diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/PriceInfoTooltip.java b/src/main/java/de/hysky/skyblocker/skyblock/item/PriceInfoTooltip.java index 0576755880..88fd678d37 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/PriceInfoTooltip.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/PriceInfoTooltip.java @@ -40,7 +40,7 @@ public class PriceInfoTooltip { private static JsonObject lowestPricesJson; private static JsonObject isMuseumJson; private static JsonObject motesPricesJson; - private static boolean nullMsgSend = false; + private static volatile boolean nullMsgSend = false; private final static Gson gson = new Gson(); private static final Map apiAddresses; private static long npcHash = 0; @@ -395,7 +395,7 @@ public static void init() { minute++; CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0])) .whenComplete((unused, throwable) -> nullMsgSend = false); - }, 1200); + }, 1200, true); } private static JsonObject downloadPrices(String type) { diff --git a/src/main/java/de/hysky/skyblocker/utils/scheduler/MessageScheduler.java b/src/main/java/de/hysky/skyblocker/utils/scheduler/MessageScheduler.java index 156369658f..a67d8da0e9 100644 --- a/src/main/java/de/hysky/skyblocker/utils/scheduler/MessageScheduler.java +++ b/src/main/java/de/hysky/skyblocker/utils/scheduler/MessageScheduler.java @@ -55,7 +55,7 @@ public void queueMessage(String message, int delay) { } @Override - protected boolean runTask(Runnable task) { + protected boolean runTask(Runnable task, boolean multithreaded) { if (lastMessage + MIN_DELAY < System.currentTimeMillis()) { task.run(); lastMessage = System.currentTimeMillis(); diff --git a/src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java b/src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java index 0f44cf9387..b254f524ef 100644 --- a/src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java +++ b/src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java @@ -1,5 +1,6 @@ package de.hysky.skyblocker.utils.scheduler; +import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.mojang.brigadier.Command; import it.unimi.dsi.fastutil.ints.AbstractInt2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; @@ -11,6 +12,8 @@ import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.function.Supplier; /** @@ -21,19 +24,35 @@ public class Scheduler { public static final Scheduler INSTANCE = new Scheduler(); private int currentTick = 0; private final AbstractInt2ObjectMap> tasks = new Int2ObjectOpenHashMap<>(); + private final ExecutorService executors = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("Skyblocker-Scheduler-%d").build()); protected Scheduler() { } + + /** + * @see #schedule(Runnable, int, boolean) + */ + public void schedule(Runnable task, int delay) { + schedule(task, delay, false); + } + + /** + * @see #scheduleCyclic(Runnable, int, boolean) + */ + public void scheduleCyclic(Runnable task, int period) { + scheduleCyclic(task, period, false); + } /** * Schedules a task to run after a delay. * * @param task the task to run * @param delay the delay in ticks + * @param multithreaded whether to run the task on the schedulers dedicated thread pool */ - public void schedule(Runnable task, int delay) { + public void schedule(Runnable task, int delay, boolean multithreaded) { if (delay >= 0) { - addTask(new ScheduledTask(task), currentTick + delay); + addTask(new ScheduledTask(task, multithreaded), currentTick + delay); } else { LOGGER.warn("Scheduled a task with negative delay"); } @@ -44,10 +63,11 @@ public void schedule(Runnable task, int delay) { * * @param task the task to run * @param period the period in ticks + * @param multithreaded whether to run the task on the schedulers dedicated thread pool */ - public void scheduleCyclic(Runnable task, int period) { + public void scheduleCyclic(Runnable task, int period, boolean multithreaded) { if (period > 0) { - addTask(new CyclicTask(task, period), currentTick); + addTask(new ScheduledTask(task, period, true, multithreaded), currentTick); } else { LOGGER.error("Attempted to schedule a cyclic task with period lower than 1"); } @@ -74,7 +94,7 @@ public void tick() { //noinspection ForLoopReplaceableByForEach (or else we get a ConcurrentModificationException) for (int i = 0; i < currentTickTasks.size(); i++) { ScheduledTask task = currentTickTasks.get(i); - if (!runTask(task)) { + if (!runTask(task, task.multithreaded)) { tasks.computeIfAbsent(currentTick + 1, key -> new ArrayList<>()).add(task); } } @@ -89,8 +109,13 @@ public void tick() { * @param task the task to run * @return {@code true} if the task is run, and {@link false} if task is not run. */ - protected boolean runTask(Runnable task) { - task.run(); + protected boolean runTask(Runnable task, boolean multithreaded) { + if (multithreaded) { + executors.execute(task); + } else { + task.run(); + } + return true; } @@ -105,36 +130,18 @@ private void addTask(ScheduledTask scheduledTask, int schedule) { } /** - * A task that runs every period ticks. More specifically, this task reschedules itself to run again after period ticks every time it runs. + * A task that that is scheduled to execute once after the {@code interval}, or that is run every {@code interval} ticks. */ - protected class CyclicTask extends ScheduledTask { - private final int period; - - CyclicTask(Runnable inner, int period) { - super(inner); - this.period = period; + protected record ScheduledTask(Runnable task, int interval, boolean cyclic, boolean multithreaded) implements Runnable { + private ScheduledTask(Runnable task, boolean multithreaded) { + this(task, -1, false, multithreaded); } @Override public void run() { - super.run(); - addTask(this, currentTick + period); - } - } + task.run(); - /** - * A task that runs at a specific tick, relative to {@link #currentTick}. - */ - protected static class ScheduledTask implements Runnable { - private final Runnable inner; - - public ScheduledTask(Runnable inner) { - this.inner = inner; - } - - @Override - public void run() { - inner.run(); + if (cyclic) INSTANCE.addTask(this, INSTANCE.currentTick + interval); } } }