Package com.glitchybyte.glib.concurrent
Class GTask
java.lang.Object
com.glitchybyte.glib.concurrent.GTask
- All Implemented Interfaces:
Runnable
- Direct Known Subclasses:
GDisplayDataTask,GProcessOutputCollectorTask,GProcessTask,GTerminalDisplayTask,GWorkQueueTask
A single concurrent task that starts in a separate thread and runs until
finished or the task is interrupted.
GTasks are long-lived or busy. Though there is no error if they
are very short-lived, a virtual thread is a better option for that. All threads
started by GTaskRunner are always platform threads.
Implementations MUST call started within their run
implementation when the task is ready to receive inputs (e.g., after
acquiring locks). Repeated calls to started are permitted and will
return fast without performing any further synchronization.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAwaits indefinitely until the task is done.protected GTaskRunnerReturns a task runner this task can use to run other tasks.Returns the given name of this task thread, or null if it wasn't set.voidInterrupts the task.booleanisDone()Returns whether this task has completed.protected voidsetTaskRunner(GTaskRunner taskRunner) Sets the task runner this task can use to run other tasks.protected voidstarted()Marks this task as started, allowing the queueing thread to continue.
-
Constructor Details
-
GTask
Creates a concurrent task with the given name, or a default name if null.- Parameters:
taskThreadName- Thread name. If null, a default unique name of the form 'Task-{NUMBER}' is used.
-
GTask
public GTask()Creates a concurrent task with a default thread name.
-
-
Method Details
-
setTaskRunner
Sets the task runner this task can use to run other tasks. On start, it defaults to the runner that is running this task.- Parameters:
taskRunner- Task runner.
-
getTaskRunner
Returns a task runner this task can use to run other tasks. On start, it defaults to the runner that is running this task.- Returns:
- A task runner this task can use to run other tasks.
-
getTaskThreadName
Returns the given name of this task thread, or null if it wasn't set. If name is not set the runner will assign a default name when creating the thread.- Returns:
- The given name of this task thread.
-
started
protected void started()Marks this task as started, allowing the queueing thread to continue.This method MUST be called to indicate the task is running and ready. Even if the task decides it will exit fast it must call this method.
Repeated calls to this method are permitted and will return fast.
-
isDone
public boolean isDone()Returns whether this task has completed.- Returns:
- Whether this task has completed.
-
awaitDone
Awaits indefinitely until the task is done.- Throws:
InterruptedException- If the wait was interrupted.
-
interrupt
public void interrupt()Interrupts the task.
-