From 1a4e3481d5f9546f1a4a4c5088d0170540582177 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Mon, 20 Feb 2023 14:19:51 +0100 Subject: [PATCH] Change list by a queue in order to use a thread-safe non-blocking concurrent queue Signed-off-by: Quentin Legot --- .../src/main/java/fr/altarik/toolbox/task/Scheduler.java | 5 +++-- .../fr/altarik/toolbox/task/async/AsyncPeriodicTasks.java | 7 ++++--- .../fr/altarik/toolbox/task/sync/PeriodicSyncTask.java | 8 ++++---- .../main/java/fr/altarik/toolbox/task/sync/SyncTask.java | 8 ++++---- gradle.properties | 2 +- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/Scheduler.java b/Tasks/src/main/java/fr/altarik/toolbox/task/Scheduler.java index c3b5d99..b6bb5d3 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/Scheduler.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/Scheduler.java @@ -2,13 +2,14 @@ package fr.altarik.toolbox.task; import java.util.ArrayList; import java.util.List; +import java.util.Queue; public class Scheduler implements Runnable { - private final List tasks; + private final Queue tasks; private final SendTaskWorkerI worker; - public Scheduler(SendTaskWorkerI worker, List tasks) { + public Scheduler(SendTaskWorkerI worker, Queue tasks) { this.worker = worker; this.tasks = tasks; } 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 b3f7b9d..3b55251 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,8 @@ package fr.altarik.toolbox.task.async; import fr.altarik.toolbox.task.*; -import java.util.Stack; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; @@ -14,7 +15,7 @@ import java.util.concurrent.TimeUnit; public class AsyncPeriodicTasks implements PeriodicTaskI, AsyncTaskI, SendTaskWorkerI { private final ExecutorService worker; - private final Stack tasks; + private final Queue tasks; protected final Scheduler scheduler; private final ServerTickListener listener; @@ -26,7 +27,7 @@ public class AsyncPeriodicTasks implements PeriodicTaskI, AsyncTaskI, SendTaskWo } else { worker = Executors.newFixedThreadPool(numberOfWorker); } - tasks = new Stack<>(); + tasks = new ConcurrentLinkedQueue<>(); this.scheduler = new Scheduler(this, tasks); this.listener = new ServerTickListener(scheduler); } 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 e0c5e1f..83a6b1f 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 @@ -2,17 +2,17 @@ package fr.altarik.toolbox.task.sync; import fr.altarik.toolbox.task.*; -import java.util.ArrayList; -import java.util.List; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; public class PeriodicSyncTask implements PeriodicTaskI, SendTaskWorkerI { private final ServerTickListener listener; - private final List tasks; + private final Queue tasks; protected final Scheduler scheduler; private PeriodicSyncTask() { - this.tasks = new ArrayList<>(2); + this.tasks = new ConcurrentLinkedQueue<>(); this.scheduler = new Scheduler(this, tasks); this.listener = new ServerTickListener(scheduler); } diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/sync/SyncTask.java b/Tasks/src/main/java/fr/altarik/toolbox/task/sync/SyncTask.java index 19214e2..e77658d 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/sync/SyncTask.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/sync/SyncTask.java @@ -2,17 +2,17 @@ package fr.altarik.toolbox.task.sync; import fr.altarik.toolbox.task.*; -import java.util.ArrayList; -import java.util.List; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; public class SyncTask implements TaskI, SendTaskWorkerI { private final ServerTickListener listener; - private final List tasks; + private final Queue tasks; protected final Scheduler scheduler; private SyncTask() { - this.tasks = new ArrayList<>(2); + this.tasks = new ConcurrentLinkedQueue<>(); this.scheduler = new Scheduler(this, tasks); this.listener = new ServerTickListener(scheduler); } diff --git a/gradle.properties b/gradle.properties index 2b3981a..b918202 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,6 +8,6 @@ loader_version=0.14.12 fabric_version=0.70.0+1.19.3 maven_group=fr.altarik.toolbox -maven_version=3.0.1-SNAPSHOT +maven_version=4.0.0-SNAPSHOT repo_username=Altarik repo_password=password \ No newline at end of file -- 2.39.5