Package com.glitchybyte.glib.concurrent
Class GTaskRunnerService
java.lang.Object
com.glitchybyte.glib.concurrent.GTaskExecutorService<ExecutorService>
com.glitchybyte.glib.concurrent.GTaskRunnerService
- All Implemented Interfaces:
GTaskRunner,AutoCloseable,Executor
public final class GTaskRunnerService
extends GTaskExecutorService<ExecutorService>
implements GTaskRunner
A task runner facility to run
GTask tasks.
Classes ending in 'Task' in the concurrent package must be started with
a GTaskRunnerService.
This runner is also capable of running standalone Runnables and
Callables. This avoids having to create, or keep track of, a separate
ExecutorService. Keep in mind this class uses platform threads.
-
Field Summary
Fields inherited from class com.glitchybyte.glib.concurrent.GTaskExecutorService
runner -
Constructor Summary
ConstructorsConstructorDescriptionCreates a task runner with an unbounded cached thread pool.GTaskRunnerService(Integer threadCount) Creates a task runner with a fixed thread pool. -
Method Summary
Modifier and TypeMethodDescription<V> CompletableFuture<V> Submits aCallableto execute concurrently.<V> List<CompletableFuture<V>> callAll(Collection<Callable<V>> callables) Submits a collection ofCallableto execute concurrently.<V> List<CompletableFuture<V>> Submits an array ofCallableto execute concurrently.Submits aRunnableto execute concurrently.Submits an array ofRunnableto execute concurrently.runAll(Collection<Runnable> runnables) Submits a collection ofRunnableto execute concurrently.<T extends GTask>
Tstart(T task) Submits a task to be run concurrently.<T extends GTask>
TSubmits a task to execute concurrently.Methods inherited from class com.glitchybyte.glib.concurrent.GTaskExecutorService
close, createTaskWrapper, execute
-
Constructor Details
-
GTaskRunnerService
public GTaskRunnerService()Creates a task runner with an unbounded cached thread pool. -
GTaskRunnerService
Creates a task runner with a fixed thread pool.- Parameters:
threadCount- Thread count for this runner.
-
-
Method Details
-
run
Description copied from interface:GTaskRunnerSubmits aRunnableto execute concurrently.- Specified by:
runin interfaceGTaskRunner- Parameters:
runnable- ARunnableto execute.- Returns:
- A
CompletableFuture<Void>representing pending completion of the task. - Throws:
RejectedExecutionException- If the task cannot be scheduled for execution.
-
runAll
public List<CompletableFuture<Void>> runAll(Collection<Runnable> runnables) throws RejectedExecutionException Description copied from interface:GTaskRunnerSubmits a collection ofRunnableto execute concurrently.- Specified by:
runAllin interfaceGTaskRunner- Parameters:
runnables- A collection ofRunnableto execute.- Returns:
- A list of
CompletableFuture<Void>representing pending completion of the tasks. - Throws:
RejectedExecutionException- If one the tasks cannot be scheduled for execution.
-
runAll
Description copied from interface:GTaskRunnerSubmits an array ofRunnableto execute concurrently.- Specified by:
runAllin interfaceGTaskRunner- Parameters:
runnables- An array ofRunnableto execute.- Returns:
- A list of
CompletableFuture<Void>representing pending completion of the tasks. - Throws:
RejectedExecutionException- If one the tasks cannot be scheduled for execution.
-
call
Description copied from interface:GTaskRunnerSubmits aCallableto execute concurrently.- Specified by:
callin interfaceGTaskRunner- Type Parameters:
V- Type of task result.- Parameters:
callable- ACallableto execute.- Returns:
- A
CompletableFuturerepresenting pending completion of the task. - Throws:
RejectedExecutionException- If the task cannot be scheduled for execution.
-
callAll
public <V> List<CompletableFuture<V>> callAll(Collection<Callable<V>> callables) throws RejectedExecutionException Description copied from interface:GTaskRunnerSubmits a collection ofCallableto execute concurrently.- Specified by:
callAllin interfaceGTaskRunner- Type Parameters:
V- Type of task result.- Parameters:
callables- A collection ofCallableto execute.- Returns:
- A list of
CompletableFuturerepresenting pending completion of the tasks. - Throws:
RejectedExecutionException- If one of the tasks cannot be scheduled for execution.
-
callAll
public <V> List<CompletableFuture<V>> callAll(Callable<V>[] callables) throws RejectedExecutionException Description copied from interface:GTaskRunnerSubmits an array ofCallableto execute concurrently.- Specified by:
callAllin interfaceGTaskRunner- Type Parameters:
V- Type of task result.- Parameters:
callables- An array ofCallableto execute.- Returns:
- A list of
CompletableFuturerepresenting pending completion of the tasks. - Throws:
RejectedExecutionException- If one the tasks cannot be scheduled for execution.
-
start
public <T extends GTask> T start(T task, Duration timeout) throws InterruptedException, RejectedExecutionException Description copied from interface:GTaskRunnerSubmits a task to execute concurrently.If the task does not start within the given timeout, an
IllegalStateExceptionwill be raised as this is considered a bug in the task implementation.This method blocks until the task has started.
- Specified by:
startin interfaceGTaskRunner- Type Parameters:
T- Task type.- Parameters:
task- Task to start.timeout- Time given to the task to start.- Returns:
- The started task.
- Throws:
InterruptedException- If the thread is interrupted while waiting for the task to start.RejectedExecutionException- If the task cannot be scheduled for execution.
-
start
Description copied from interface:GTaskRunnerSubmits a task to be run concurrently.If the task does not start within 5 seconds, an
IllegalStateExceptionwill be raised as this is considered a bug in the task implementation.This method blocks until the task has started.
- Specified by:
startin interfaceGTaskRunner- Type Parameters:
T- Task type.- Parameters:
task- Task to start.- Returns:
- The started task.
- Throws:
InterruptedException- If the thread is interrupted while waiting for the task to start.RejectedExecutionException- If the task cannot be scheduled for execution.
-