fabric-synctask #1

Merged
quentinlegot merged 10 commits from fabric-synctask into master 2023-02-13 15:26:38 +01:00
5 changed files with 69 additions and 4 deletions
Showing only changes of commit f5db55e96e - Show all commits

View File

@ -10,5 +10,5 @@ public interface PeriodicTaskI extends TaskI {
* @throws InterruptedException When executed asynchronously, task may be interrupted * @throws InterruptedException When executed asynchronously, task may be interrupted
* @see fr.altarik.toolbox.task.sync.PeriodicSyncTask * @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;
} }

View File

@ -2,5 +2,10 @@ package fr.altarik.toolbox.task;
public interface SendTaskWorkerI { 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); void sendTask(AltarikRunnable task);
} }

View File

@ -1,15 +1,50 @@
package fr.altarik.toolbox.task; 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.async.AsyncTasks;
import fr.altarik.toolbox.task.sync.PeriodicSyncTask;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
public class Task implements 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 @Override
public void onInitialize() { 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() { public TaskI getAsyncWorkers() {

View File

@ -2,6 +2,11 @@ package fr.altarik.toolbox.task;
public interface TaskI { 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; void addTask(AltarikRunnable function) throws InterruptedException;
} }

View File

@ -7,6 +7,10 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; 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 { public class AsyncPeriodicTasks implements PeriodicTaskI, AsyncTaskI, SendTaskWorkerI {
private final ExecutorService worker; private final ExecutorService worker;
@ -41,11 +45,23 @@ public class AsyncPeriodicTasks implements PeriodicTaskI, AsyncTaskI, SendTaskWo
return initialize(Runtime.getRuntime().availableProcessors()); 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 @Override
public void addTask(AltarikRunnable function) throws InterruptedException { 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 @Override
public void addTask(AltarikRunnable function, long delay, long period) throws InterruptedException { public void addTask(AltarikRunnable function, long delay, long period) throws InterruptedException {
if(worker.isTerminated() || worker.isShutdown()) { if(worker.isTerminated() || worker.isShutdown()) {
@ -54,6 +70,10 @@ public class AsyncPeriodicTasks implements PeriodicTaskI, AsyncTaskI, SendTaskWo
tasks.add(new SchedulerTaskData(function, delay, period - 1)); 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 @Override
public void close() throws Exception { public void close() throws Exception {
worker.shutdown(); worker.shutdown();