diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 439a5fb..a122ba0 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,8 +4,7 @@
-
Method used to add your task to a list of task, task are stored in a FIFO(First-In First-Out) implementation ({@link java.util.concurrent.BlockingQueue}). + * As BlockingQueue is a synchronized class, all operations to add or remove elements inside cannot have collisions issues
+ *Example:
+ *+ * for(int i = 0; i < 4; i++) { + * System.out.println("task " + i); + * AtomicInteger atomicI = new AtomicInteger(i); + * AsyncTasks.addTask(() -> { + * System.out.println(i); + * } + * } + * + *+ *
Result(output may differ):
+ *+ * task 0 + * task 1 + * 0 + * task 2 + * 1 + * 2 + * task 3 + * 3 + *+ * The worker thread is sleeping if it doesn't have task to execute and wake up if necessary when you add a task + * @param function task to be executed + * @throws InterruptedException when worker thread or BlockQueue has been interrupted while waiting (which is anormal) + */ public static void addTask(Runnable function) throws InterruptedException { if(worker.workerThread.isInterrupted()) throw new InterruptedException("Async task thread has been interrupted while waiting for another task, which is anormal, please report this issue to developers"); @@ -20,6 +56,10 @@ public class AsyncTasks { } } + /** + * Return the numbers of produced tasks which hasn't been consumed yet + * @return numbers of produced tasks which hasn't been consumed yet + */ public static int numberOfWaitingTask() { return worker.tasks.size(); } diff --git a/src/main/java/fr/altarik/toolbox/asynctasks/TasksThread.java b/src/main/java/fr/altarik/toolbox/asynctasks/TasksThread.java index 2f77ce9..96e0ebb 100644 --- a/src/main/java/fr/altarik/toolbox/asynctasks/TasksThread.java +++ b/src/main/java/fr/altarik/toolbox/asynctasks/TasksThread.java @@ -5,7 +5,10 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; -public class TasksThread { +/** + * Package private class, AsyncTasks is the interface user need to interact with it and users should never directly call this class + */ +class TasksThread { boolean isWaiting = false; final ReentrantLock lock = new ReentrantLock(); @@ -14,7 +17,7 @@ public class TasksThread { final BlockingQueue