Class GTask

java.lang.Object
com.glitchybyte.glib.concurrent.GTask
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
GDisplayDataTask, GProcessOutputCollectorTask, GProcessTask, GTerminalDisplayTask, GWorkQueueTask

public abstract class GTask extends Object implements Runnable
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
    Constructor
    Description
    Creates a concurrent task with a default thread name.
    GTask(String taskThreadName)
    Creates a concurrent task with the given name, or a default name if null.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Awaits indefinitely until the task is done.
    protected GTaskRunner
    Returns 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.
    void
    Interrupts the task.
    boolean
    Returns whether this task has completed.
    protected void
    Sets the task runner this task can use to run other tasks.
    protected void
    Marks this task as started, allowing the queueing thread to continue.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.lang.Runnable

    run
  • Constructor Details

    • GTask

      public GTask(String taskThreadName)
      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

      protected void setTaskRunner(GTaskRunner taskRunner)
      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

      protected GTaskRunner 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

      public String 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

      public void awaitDone() throws InterruptedException
      Awaits indefinitely until the task is done.
      Throws:
      InterruptedException - If the wait was interrupted.
    • interrupt

      public void interrupt()
      Interrupts the task.