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 2a7cd48..fd65aa2 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/PeriodicTaskI.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/PeriodicTaskI.java @@ -10,5 +10,5 @@ public interface PeriodicTaskI extends TaskI { * @throws InterruptedException When executed asynchronously, task may be interrupted * @see fr.altarik.toolbox.task.sync.PeriodicSyncTask */ - public void addTask(AltarikRunnable function, long delay, long period) throws InterruptedException; + void addTask(AltarikRunnable function, long delay, long period) throws InterruptedException; } diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/SendTaskWorkerI.java b/Tasks/src/main/java/fr/altarik/toolbox/task/SendTaskWorkerI.java index 01f354d..247ff5b 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/SendTaskWorkerI.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/SendTaskWorkerI.java @@ -2,5 +2,10 @@ package fr.altarik.toolbox.task; public interface SendTaskWorkerI { + /** + * Internal use for scheduler, do not use. + * Scheduler use this method to send the task to execute to worker + * @param task task to execute now + */ void sendTask(AltarikRunnable task); } 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 b88d067..c9b8129 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/Task.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/Task.java @@ -1,15 +1,50 @@ package fr.altarik.toolbox.task; +import fr.altarik.toolbox.task.async.AsyncPeriodicTasks; +import fr.altarik.toolbox.task.async.AsyncTaskI; import fr.altarik.toolbox.task.async.AsyncTasks; +import fr.altarik.toolbox.task.sync.PeriodicSyncTask; import net.fabricmc.api.ModInitializer; public class Task implements ModInitializer { - public TaskI asyncWorkers = AsyncTasks.initialize(); + public final TaskI asyncWorkers = AsyncTasks.initialize(); + public final PeriodicTaskI periodicSyncTask = PeriodicSyncTask.initialize(); + + public final AsyncTaskI asyncTasks = AsyncTasks.initialize(); + + public final PeriodicTaskI periodicAsyncTask = AsyncPeriodicTasks.initialize(); @Override public void onInitialize() { - + /*try { + asyncWorkers.addTask(new AltarikRunnable() { + @Override + public void run() { + System.out.println("Hello world 1"); + } + }); + periodicSyncTask.addTask(new AltarikRunnable() { + @Override + public void run() { + System.out.println("Hello world 2"); + } + }, 40, 60); + asyncTasks.addTask(new AltarikRunnable() { + @Override + public void run() { + System.out.println("Hello world 3 : " + Thread.currentThread().getName()); + } + }); + periodicAsyncTask.addTask(new AltarikRunnable() { + @Override + public void run() { + System.out.println("Hello world 4 : " + Thread.currentThread().getName()); + } + }, 60, 80); + } catch (InterruptedException e) { + throw new RuntimeException(e); + }*/ } public TaskI getAsyncWorkers() { diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/TaskI.java b/Tasks/src/main/java/fr/altarik/toolbox/task/TaskI.java index 2f668a8..6a22e48 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/TaskI.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/TaskI.java @@ -2,6 +2,11 @@ package fr.altarik.toolbox.task; public interface TaskI { + /** + * Send task to worker, execution depends on implementation + * @param function task you send to worker + * @throws InterruptedException used by asynchronous workers if threads has been interrupted or shutdown + */ void addTask(AltarikRunnable function) throws InterruptedException; } 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 1e095f8..b3f7b9d 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 @@ -7,6 +7,10 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; +/** + * A task manager to execute periodic tasks asynchronously. A scheduler on the main/server thread will send the task to + * worker threads. + */ public class AsyncPeriodicTasks implements PeriodicTaskI, AsyncTaskI, SendTaskWorkerI { private final ExecutorService worker; @@ -41,11 +45,23 @@ public class AsyncPeriodicTasks implements PeriodicTaskI, AsyncTaskI, SendTaskWo return initialize(Runtime.getRuntime().availableProcessors()); } + /** + * Send the task to the scheduler, the task is executed at the next server tick and at every following tick + * @param function the function which will be executed + * @throws InterruptedException if worker has terminated or is shutting down + */ @Override public void addTask(AltarikRunnable function) throws InterruptedException { - this.addTask(function, 0, 1000); + this.addTask(function, 0, 1); } + /** + * Send the task to the scheduler, executed depending on the parameters (delay and period) + * @param function the function to execute + * @param delay delay in tick before starting the task + * @param period time in tick to wait between runs + * @throws InterruptedException if worker has terminated or is shutting down + */ @Override public void addTask(AltarikRunnable function, long delay, long period) throws InterruptedException { if(worker.isTerminated() || worker.isShutdown()) { @@ -54,6 +70,10 @@ public class AsyncPeriodicTasks implements PeriodicTaskI, AsyncTaskI, SendTaskWo tasks.add(new SchedulerTaskData(function, delay, period - 1)); } + /** + * Try to execute task you already send in 10 seconds, otherwise workers are killed. + * @throws AsyncTasks.UnfinishedTasksException if workers has been shutdown before finishing every tasks + */ @Override public void close() throws Exception { worker.shutdown();