Package com.jozufozu.flywheel.impl.task
Class ParallelTaskExecutor
java.lang.Object
com.jozufozu.flywheel.impl.task.ParallelTaskExecutor
- All Implemented Interfaces:
TaskExecutor,Executor
-
Constructor Summary
ConstructorsConstructorDescriptionParallelTaskExecutor(String name, int threadCount, BooleanSupplier mainThreadQuery) -
Method Summary
Modifier and TypeMethodDescriptionvoidbooleanCheck whether the current thread is this task executor's main thread.voidscheduleForMainThread(Runnable runnable) Schedule a task to be run on the main thread.voidSpawns a number of work-stealing threads to process results in the task queue.voidvoidWait for all running tasks to finish.booleansyncUntil(BooleanSupplier cond) Wait for running tasks, until the given condition is met (BooleanSupplier.getAsBoolean()returnstrue).booleansyncWhile(BooleanSupplier cond) Wait for running tasks, so long as the given condition is met (BooleanSupplier.getAsBoolean()returnstrue).intCheck for the number of threads this executor uses.
-
Constructor Details
-
ParallelTaskExecutor
-
-
Method Details
-
threadCount
public int threadCount()Description copied from interface:TaskExecutorCheck for the number of threads this executor uses.
May be helpful when determining how many chunks to divide a task into.- Specified by:
threadCountin interfaceTaskExecutor- Returns:
- The number of threads this executor uses.
-
startWorkers
public void startWorkers()Spawns a number of work-stealing threads to process results in the task queue. If the executor is already running, this method does nothing and exits. -
stopWorkers
public void stopWorkers() -
execute
-
scheduleForMainThread
Description copied from interface:TaskExecutorSchedule a task to be run on the main thread.
This method may be called from any thread (including the main thread), but the runnable will only be executed once somebody calls eitherTaskExecutor.syncPoint()orTaskExecutor.syncUntil(BooleanSupplier)on this task executor's main thread.- Specified by:
scheduleForMainThreadin interfaceTaskExecutor- Parameters:
runnable- The task to run.
-
isMainThread
public boolean isMainThread()Description copied from interface:TaskExecutorCheck whether the current thread is this task executor's main thread.- Specified by:
isMainThreadin interfaceTaskExecutor- Returns:
trueif the current thread is the main thread.
-
syncPoint
public void syncPoint()Wait for all running tasks to finish.- Specified by:
syncPointin interfaceTaskExecutor
-
syncUntil
Description copied from interface:TaskExecutorWait for running tasks, until the given condition is met (BooleanSupplier.getAsBoolean()returnstrue).
This method is equivalent tosyncWhile(() -> !cond.getAsBoolean()).- Specified by:
syncUntilin interfaceTaskExecutor- Parameters:
cond- The condition to wait for.- Returns:
trueif the condition is met.falseif this executor runs out of tasks before the condition is met.
-
syncWhile
Description copied from interface:TaskExecutorWait for running tasks, so long as the given condition is met (BooleanSupplier.getAsBoolean()returnstrue).
If this method is called on the
This method is equivalent tosyncUntil(() -> !cond.getAsBoolean()).- Specified by:
syncWhilein interfaceTaskExecutor- Parameters:
cond- The condition sync on.- Returns:
trueif the condition is no longer met.falseif this executor runs out of tasks while the condition is still met.
-