diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/async/AsyncPeriodicTasks.java b/Tasks/src/main/java/fr/altarik/toolbox/task/async/AsyncPeriodicTasks.java index 46262b1..1141751 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/async/AsyncPeriodicTasks.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/async/AsyncPeriodicTasks.java @@ -2,7 +2,6 @@ package fr.altarik.toolbox.task.async; 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; @@ -48,11 +47,11 @@ public class AsyncPeriodicTasks implements PeriodicTaskI, AsyncTaskI { * * @return an instance of AsyncTasks */ - public static TaskI initialize(int numberOfWorker) { + public static PeriodicTaskI initialize(int numberOfWorker) { return new AsyncPeriodicTasks(numberOfWorker); } - public static TaskI initialize() { + public static PeriodicTaskI initialize() { return initialize(Runtime.getRuntime().availableProcessors()); } diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/async/TaskScheduler.java b/Tasks/src/main/java/fr/altarik/toolbox/task/async/TaskScheduler.java index 04358ae..4bc2e74 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/async/TaskScheduler.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/async/TaskScheduler.java @@ -30,7 +30,6 @@ public class TaskScheduler { */ public synchronized void asyncRunnerPeriodicTasks() throws InterruptedException { loop: while(true) { - notify(); while(asyncTasks.size() == 0) { if(isStop()) { break loop; 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 index 25d2e10..381c263 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/sync/PeriodicSyncTask.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/sync/PeriodicSyncTask.java @@ -24,7 +24,7 @@ public class PeriodicSyncTask implements PeriodicTaskI, Runnable { @Override public void addTask(AltarikRunnable function) { - addTask(function, 0, 0); + addTask(function, 0, 1); } @Override @@ -51,6 +51,6 @@ public class PeriodicSyncTask implements PeriodicTaskI, Runnable { @Override public void addTask(AltarikRunnable function, long delay, long period) { - tasks.add(new SchedulerTaskData(function, delay, period)); + tasks.add(new SchedulerTaskData(function, delay, period - 1)); } } diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/sync/OneTimeSyncTask.java b/Tasks/src/main/java/fr/altarik/toolbox/task/sync/SyncTask.java similarity index 91% rename from Tasks/src/main/java/fr/altarik/toolbox/task/sync/OneTimeSyncTask.java rename to Tasks/src/main/java/fr/altarik/toolbox/task/sync/SyncTask.java index 0beeda1..749fe0c 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/sync/OneTimeSyncTask.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/sync/SyncTask.java @@ -7,18 +7,18 @@ import fr.altarik.toolbox.task.TaskI; import java.util.ArrayList; import java.util.List; -public class OneTimeSyncTask implements TaskI, Runnable { +public class SyncTask implements TaskI, Runnable { private final ServerTickListener listener; private final List tasks; - private OneTimeSyncTask() { + private SyncTask() { this.listener = new ServerTickListener(this); this.tasks = new ArrayList<>(2); } public static TaskI initialize() { - return new OneTimeSyncTask(); + return new SyncTask(); } @Override diff --git a/Tasks/src/test/java/fr/altarik/toolbox/task/async/PeriodicAsyncTaskTest.java b/Tasks/src/test/java/fr/altarik/toolbox/task/async/PeriodicAsyncTaskTest.java new file mode 100644 index 0000000..29e0fe6 --- /dev/null +++ b/Tasks/src/test/java/fr/altarik/toolbox/task/async/PeriodicAsyncTaskTest.java @@ -0,0 +1,32 @@ +package fr.altarik.toolbox.task.async; + +import fr.altarik.toolbox.task.AltarikRunnable; +import org.junit.jupiter.api.Test; + +import java.util.Stack; +import java.util.concurrent.atomic.AtomicInteger; + +public class PeriodicAsyncTaskTest { + + @Test + void testPeriodicASyncTask() { + AsyncPeriodicTasks worker = (AsyncPeriodicTasks) AsyncPeriodicTasks.initialize(); + Stack results = new Stack<>(); + AtomicInteger value1 = new AtomicInteger(1); + AtomicInteger value2 = new AtomicInteger(2); + AltarikRunnable runnable1 = new AltarikRunnable() { + @Override + public void run() { + results.add(value1); + } + }; + AltarikRunnable runnable2 = new AltarikRunnable() { + @Override + public void run() { + results.add(value2); + } + }; + + + } +} diff --git a/Tasks/src/test/java/fr/altarik/toolbox/task/sync/OneTimeSyncTaskTest.java b/Tasks/src/test/java/fr/altarik/toolbox/task/sync/OneTimeSyncTaskTest.java deleted file mode 100644 index e9e8a1f..0000000 --- a/Tasks/src/test/java/fr/altarik/toolbox/task/sync/OneTimeSyncTaskTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package fr.altarik.toolbox.task.sync; - -import fr.altarik.toolbox.task.AltarikRunnable; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; - -public class OneTimeSyncTaskTest { - - @Test - void testOneTimeTask() { - OneTimeSyncTask worker = (OneTimeSyncTask) OneTimeSyncTask.initialize(); - List results = new ArrayList<>(); - AtomicInteger value1 = new AtomicInteger(1); - AtomicInteger value2 = new AtomicInteger(2); - worker.addTask(new AltarikRunnable() { - @Override - public void run() { - results.add(value1); - } - }); - } -} diff --git a/Tasks/src/test/java/fr/altarik/toolbox/task/sync/PeriodicSyncTaskTest.java b/Tasks/src/test/java/fr/altarik/toolbox/task/sync/PeriodicSyncTaskTest.java index b5d0a90..2a5565c 100644 --- a/Tasks/src/test/java/fr/altarik/toolbox/task/sync/PeriodicSyncTaskTest.java +++ b/Tasks/src/test/java/fr/altarik/toolbox/task/sync/PeriodicSyncTaskTest.java @@ -8,7 +8,7 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; -public class PeriodicSyncTaskTest { +class PeriodicSyncTaskTest { @Test void testPeriodicSyncTask() { @@ -22,7 +22,7 @@ public class PeriodicSyncTaskTest { public void run() { results.add(value1); } - }, 1, 2); + }, 1, 3); worker.addTask(new AltarikRunnable() { private int i = 0; @Override diff --git a/Tasks/src/test/java/fr/altarik/toolbox/task/sync/SyncTaskTest.java b/Tasks/src/test/java/fr/altarik/toolbox/task/sync/SyncTaskTest.java new file mode 100644 index 0000000..b700843 --- /dev/null +++ b/Tasks/src/test/java/fr/altarik/toolbox/task/sync/SyncTaskTest.java @@ -0,0 +1,49 @@ +package fr.altarik.toolbox.task.sync; + +import fr.altarik.toolbox.task.AltarikRunnable; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +class SyncTaskTest { + + @Test + void testOneTimeTask() { + SyncTask worker = (SyncTask) SyncTask.initialize(); + List results = new ArrayList<>(); + AtomicInteger value1 = new AtomicInteger(1); + AtomicInteger value2 = new AtomicInteger(2); + AltarikRunnable task1 = new AltarikRunnable() { + @Override + public void run() { + results.add(value1); + } + }; + AltarikRunnable task2 = new AltarikRunnable() { + @Override + public void run() { + results.add(value2); + } + }; + worker.addTask(task1); + worker.run(); + worker.run(); + worker.addTask(task2); + worker.addTask(task1); + worker.addTask(task2); + worker.run(); + worker.addTask(task1); + worker.run(); + AtomicInteger[] expected = { + value1, + value2, + value1, + value2, + value1 + }; + Assertions.assertArrayEquals(expected, results.toArray()); + } +}