diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/AltarikRunnable.java b/Tasks/src/main/java/fr/altarik/toolbox/task/AltarikRunnable.java index 8941675..bdc3470 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/AltarikRunnable.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/AltarikRunnable.java @@ -5,7 +5,7 @@ public abstract class AltarikRunnable implements Runnable { private boolean isCancelled = false; /** - * Warning: Some task cannot be cancelled (mostly async tasks like {@link fr.altarik.toolbox.task.asyncTasks.AsyncTasks} + * Warning: Some task cannot be cancelled (mostly async tasks like {@link fr.altarik.toolbox.task.async.AsyncTasks} * The result of this call is ignored in this case, you can still add a way to not execute its content (like if(isCancelled) return;) */ public void cancel() { diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/AsyncTaskI.java b/Tasks/src/main/java/fr/altarik/toolbox/task/AsyncTaskI.java deleted file mode 100644 index c5b771d..0000000 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/AsyncTaskI.java +++ /dev/null @@ -1,4 +0,0 @@ -package fr.altarik.toolbox.task; - -public interface AsyncTaskI extends TaskI, AutoCloseable { -} diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/PeriodicTaskI.java b/Tasks/src/main/java/fr/altarik/toolbox/task/PeriodicTaskI.java index 67ece7d..2a7cd48 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/PeriodicTaskI.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/PeriodicTaskI.java @@ -8,7 +8,7 @@ public interface PeriodicTaskI extends TaskI { * @param delay delay before starting the task * @param period time to wait between runs * @throws InterruptedException When executed asynchronously, task may be interrupted - * @see fr.altarik.toolbox.task.syncTasks.PeriodicSyncTask + * @see fr.altarik.toolbox.task.sync.PeriodicSyncTask */ public void addTask(AltarikRunnable function, long delay, long period) throws InterruptedException; } diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/syncTasks/SchedulerTaskData.java b/Tasks/src/main/java/fr/altarik/toolbox/task/SchedulerTaskData.java similarity index 52% rename from Tasks/src/main/java/fr/altarik/toolbox/task/syncTasks/SchedulerTaskData.java rename to Tasks/src/main/java/fr/altarik/toolbox/task/SchedulerTaskData.java index 2eb5ee8..7a11f15 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/syncTasks/SchedulerTaskData.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/SchedulerTaskData.java @@ -1,15 +1,29 @@ -package fr.altarik.toolbox.task.syncTasks; - -import fr.altarik.toolbox.task.AltarikRunnable; +package fr.altarik.toolbox.task; public class SchedulerTaskData { + /** + * Delay before executing the function for the first time + * Correspond to tick in synchronous context and milliseconds in asynchronous context + */ private final long delay; + /** + * Period of time before re-executing the function + * Correspond to tick in synchronous context and milliseconds in asynchronous context + */ private final long period; private final AltarikRunnable function; private long currentDelay; + /** + * + * Delay and Period times corresponds to tick in synchronous context and milliseconds in asynchronous context + * + * @param function instructions to execute + * @param delay Delay before executing the function for the first time + * @param period Period of time before re-executing the function + */ public SchedulerTaskData(AltarikRunnable function, long delay, long period) { this.function = function; this.delay = delay; @@ -36,4 +50,6 @@ public class SchedulerTaskData { public long getPeriod() { return period; } + + } diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/Task.java b/Tasks/src/main/java/fr/altarik/toolbox/task/Task.java index f0a0ff4..b88d067 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/Task.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/Task.java @@ -1,6 +1,6 @@ package fr.altarik.toolbox.task; -import fr.altarik.toolbox.task.asyncTasks.AsyncTasks; +import fr.altarik.toolbox.task.async.AsyncTasks; import net.fabricmc.api.ModInitializer; public class Task implements ModInitializer { diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/asyncTasks/AsyncPeriodicTasks.java b/Tasks/src/main/java/fr/altarik/toolbox/task/async/AsyncPeriodicTasks.java similarity index 93% rename from Tasks/src/main/java/fr/altarik/toolbox/task/asyncTasks/AsyncPeriodicTasks.java rename to Tasks/src/main/java/fr/altarik/toolbox/task/async/AsyncPeriodicTasks.java index de31e44..46262b1 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/asyncTasks/AsyncPeriodicTasks.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/async/AsyncPeriodicTasks.java @@ -1,6 +1,8 @@ -package fr.altarik.toolbox.task.asyncTasks; +package fr.altarik.toolbox.task.async; -import fr.altarik.toolbox.task.*; +import fr.altarik.toolbox.task.AltarikRunnable; +import fr.altarik.toolbox.task.PeriodicTaskI; +import fr.altarik.toolbox.task.TaskI; import it.unimi.dsi.fastutil.ints.IntComparators; import java.util.ArrayList; @@ -56,7 +58,7 @@ public class AsyncPeriodicTasks implements PeriodicTaskI, AsyncTaskI { @Override public void addTask(AltarikRunnable function) throws InterruptedException { - this.addTask(function, 0, 1); + this.addTask(function, 0, 1000); } @Override diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/async/AsyncTaskI.java b/Tasks/src/main/java/fr/altarik/toolbox/task/async/AsyncTaskI.java new file mode 100644 index 0000000..5084971 --- /dev/null +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/async/AsyncTaskI.java @@ -0,0 +1,6 @@ +package fr.altarik.toolbox.task.async; + +import fr.altarik.toolbox.task.TaskI; + +public interface AsyncTaskI extends TaskI, AutoCloseable { +} diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/asyncTasks/AsyncTasks.java b/Tasks/src/main/java/fr/altarik/toolbox/task/async/AsyncTasks.java similarity index 98% rename from Tasks/src/main/java/fr/altarik/toolbox/task/asyncTasks/AsyncTasks.java rename to Tasks/src/main/java/fr/altarik/toolbox/task/async/AsyncTasks.java index 50fcc43..29efd5c 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/asyncTasks/AsyncTasks.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/async/AsyncTasks.java @@ -1,4 +1,4 @@ -package fr.altarik.toolbox.task.asyncTasks; +package fr.altarik.toolbox.task.async; import fr.altarik.toolbox.task.AltarikRunnable; import fr.altarik.toolbox.task.TaskI; diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/TaskScheduler.java b/Tasks/src/main/java/fr/altarik/toolbox/task/async/TaskScheduler.java similarity index 57% rename from Tasks/src/main/java/fr/altarik/toolbox/task/TaskScheduler.java rename to Tasks/src/main/java/fr/altarik/toolbox/task/async/TaskScheduler.java index f990081..04358ae 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/TaskScheduler.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/async/TaskScheduler.java @@ -1,16 +1,27 @@ -package fr.altarik.toolbox.task; +package fr.altarik.toolbox.task.async; -import fr.altarik.toolbox.task.syncTasks.SchedulerTaskData; +import fr.altarik.toolbox.task.AltarikRunnable; +import fr.altarik.toolbox.task.SchedulerTaskData; +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.HashMap; import java.util.Vector; public class TaskScheduler { private Vector asyncTasks; + + /** + * Return last time the method was executed, Value initialized to now when using sending the task to scheduler + */ + private HashMap lastTimeExecution; private boolean stop = false; public synchronized void sendAsyncTask(AltarikRunnable function, long delay, long period) throws InterruptedException { - asyncTasks.addElement(new SchedulerTaskData(function, delay, period)); + SchedulerTaskData data = new SchedulerTaskData(function, delay, period); + asyncTasks.addElement(data); + lastTimeExecution.put(new SchedulerTaskData(function, delay, period), Instant.now()); notify(); } @@ -30,12 +41,13 @@ public class TaskScheduler { asyncTasks.remove(data); if(!data.getFunction().isCancelled()) { long currentDelay = data.getCurrentDelay(); - if(currentDelay != 0) { - data.setCurrentDelay(currentDelay - 1); - asyncTasks.addElement(data); - } else { + Instant currentTime = Instant.now(); + Instant lastExecution = lastTimeExecution.get(data); + // (lastExec + delay) - currentTime + if(lastExecution.plus(currentDelay, ChronoUnit.MILLIS).isBefore(currentTime)) { data.getFunction().run(); data.setCurrentDelay(data.getPeriod()); + lastTimeExecution.put(data, Instant.now()); asyncTasks.addElement(data); } } diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/sync/PeriodicSyncTask.java b/Tasks/src/main/java/fr/altarik/toolbox/task/sync/PeriodicSyncTask.java new file mode 100644 index 0000000..c5cee4e --- /dev/null +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/sync/PeriodicSyncTask.java @@ -0,0 +1,57 @@ +package fr.altarik.toolbox.task.sync; + +import fr.altarik.toolbox.task.AltarikRunnable; +import fr.altarik.toolbox.task.PeriodicTaskI; +import fr.altarik.toolbox.task.SchedulerTaskData; +import fr.altarik.toolbox.task.TaskI; + +import java.util.ArrayList; +import java.util.List; + +public class PeriodicSyncTask implements PeriodicTaskI, Runnable { + + private ServerTickListener listener; + private List tasks; + + private PeriodicSyncTask() { + this.listener = new ServerTickListener(this); + this.tasks = new ArrayList<>(2); + } + + + public static TaskI initialize() { + return new PeriodicSyncTask(); + } + + @Override + public void addTask(AltarikRunnable function) throws InterruptedException { + addTask(function, 0, 1); + } + + @Override + public void run() { + List removeList = new ArrayList<>(tasks.size()); + for(SchedulerTaskData data : tasks) { + if(!data.getFunction().isCancelled()) { + long currentDelay = data.getCurrentDelay(); + if(currentDelay != 0) { + data.setCurrentDelay(currentDelay - 1); + } else { + data.getFunction().run(); + data.setCurrentDelay(data.getPeriod()); + } + } else { + removeList.add(data); + } + } + for(SchedulerTaskData toRemove : removeList) { + tasks.remove(toRemove); + } + + } + + @Override + public void addTask(AltarikRunnable function, long delay, long period) throws InterruptedException { + tasks.add(new SchedulerTaskData(function, delay, period)); + } +} diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/syncTasks/ServerTickListener.java b/Tasks/src/main/java/fr/altarik/toolbox/task/sync/ServerTickListener.java similarity index 88% rename from Tasks/src/main/java/fr/altarik/toolbox/task/syncTasks/ServerTickListener.java rename to Tasks/src/main/java/fr/altarik/toolbox/task/sync/ServerTickListener.java index 663114f..a1ae23c 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/syncTasks/ServerTickListener.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/sync/ServerTickListener.java @@ -1,4 +1,4 @@ -package fr.altarik.toolbox.task.syncTasks; +package fr.altarik.toolbox.task.sync; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.minecraft.server.MinecraftServer; @@ -13,7 +13,7 @@ public class ServerTickListener { } private void onServerTick(MinecraftServer minecraftServer) { - + task.run(); } } diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/syncTasks/PeriodicSyncTask.java b/Tasks/src/main/java/fr/altarik/toolbox/task/syncTasks/PeriodicSyncTask.java deleted file mode 100644 index 0c15f4e..0000000 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/syncTasks/PeriodicSyncTask.java +++ /dev/null @@ -1,48 +0,0 @@ -package fr.altarik.toolbox.task.syncTasks; - -import fr.altarik.toolbox.task.AltarikRunnable; -import fr.altarik.toolbox.task.PeriodicTaskI; -import fr.altarik.toolbox.task.TaskI; - -import java.util.ArrayList; -import java.util.List; - -public class PeriodicSyncTask implements PeriodicTaskI, Runnable { - - private ServerTickListener listener; - private List tasks; - - private PeriodicSyncTask() { - this.listener = new ServerTickListener(this); - this.tasks = new ArrayList<>(2); - } - - - public static TaskI initialize() { - return new PeriodicSyncTask(); - } - - @Override - public void addTask(AltarikRunnable function) throws InterruptedException { - tasks.add(function); - } - - @Override - public void run() { - List removeList = new ArrayList<>(tasks.size()); - for(AltarikRunnable task : tasks) { - if(task.isCancelled()) { - removeList.add(task); - } else { - - task.run(); - } - } - tasks.removeAll(removeList); - } - - @Override - public void addTask(AltarikRunnable function, long delay, long period) throws InterruptedException { - - } -} diff --git a/Tasks/src/test/java/fr/altarik/toolbox/AsyncTaskTest.java b/Tasks/src/test/java/fr/altarik/toolbox/AsyncTaskTest.java index 6dd8593..e6169e1 100644 --- a/Tasks/src/test/java/fr/altarik/toolbox/AsyncTaskTest.java +++ b/Tasks/src/test/java/fr/altarik/toolbox/AsyncTaskTest.java @@ -2,7 +2,7 @@ package fr.altarik.toolbox; import fr.altarik.toolbox.task.AltarikRunnable; import fr.altarik.toolbox.task.TaskI; -import fr.altarik.toolbox.task.asyncTasks.AsyncTasks; +import fr.altarik.toolbox.task.async.AsyncTasks; import org.junit.jupiter.api.Test; import java.util.Date;