Class PrioritisedQueueExecutorThread

java.lang.Object
java.lang.Thread
ca.spottedleaf.concurrentutil.executor.thread.PrioritisedQueueExecutorThread
All Implemented Interfaces:
PrioritisedExecutor, Runnable

public class PrioritisedQueueExecutorThread extends Thread implements PrioritisedExecutor
Thread which will continuously drain from a specified queue.

Note: When using this thread, queue additions to the underlying queue are not sufficient to get this thread to execute the task. The function notifyTasks() must be used after scheduling a task. For expected behaviour of task scheduling, use the methods provided on this class to schedule tasks.

  • Field Details

    • queue

      protected final PrioritisedExecutor queue
    • threadShutdown

      protected volatile boolean threadShutdown
    • threadParked

      protected volatile boolean threadParked
    • THREAD_PARKED_HANDLE

      protected static final VarHandle THREAD_PARKED_HANDLE
    • halted

      protected volatile boolean halted
    • spinWaitTime

      protected final long spinWaitTime
    • DEFAULT_SPINWAIT_TIME

      protected static final long DEFAULT_SPINWAIT_TIME
      See Also:
  • Constructor Details

    • PrioritisedQueueExecutorThread

      public PrioritisedQueueExecutorThread(PrioritisedExecutor queue)
    • PrioritisedQueueExecutorThread

      public PrioritisedQueueExecutorThread(PrioritisedExecutor queue, long spinWaitTime)
  • Method Details

    • run

      public final void run()
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
    • doRun

      public final void doRun()
    • begin

      protected void begin()
    • die

      protected void die()
    • pollTasks

      protected boolean pollTasks()
      Attempts to poll as many tasks as possible, returning when finished.
      Returns:
      Whether any tasks were executed.
    • handleClose

      protected boolean handleClose()
    • notifyTasks

      public boolean notifyTasks()
      Notify this thread that a task has been added to its queue
      Returns:
      true if this thread was waiting for tasks, false if it is executing tasks
    • getTotalTasksExecuted

      public long getTotalTasksExecuted()
      Description copied from interface: PrioritisedExecutor
      Returns the number of tasks that have been executed.
      Specified by:
      getTotalTasksExecuted in interface PrioritisedExecutor
    • getTotalTasksScheduled

      public long getTotalTasksScheduled()
      Description copied from interface: PrioritisedExecutor
      Returns the number of tasks that have been scheduled are pending to be scheduled.
      Specified by:
      getTotalTasksScheduled in interface PrioritisedExecutor
    • generateNextSubOrder

      public long generateNextSubOrder()
      Description copied from interface: PrioritisedExecutor
      Generates the next suborder id.
      Specified by:
      generateNextSubOrder in interface PrioritisedExecutor
      Returns:
      The next suborder id.
    • shutdown

      public boolean shutdown()
      Description copied from interface: PrioritisedExecutor
      Prevent further additions to this executor. Attempts to add after this call has completed (potentially during) will result in IllegalStateException being thrown.

      This operation is atomic with respect to other shutdown calls

      After this call has completed, regardless of return value, this executor will be shutdown.

      Specified by:
      shutdown in interface PrioritisedExecutor
      Returns:
      true if the executor was shutdown, false if it has shut down already
      See Also:
    • isShutdown

      public boolean isShutdown()
      Description copied from interface: PrioritisedExecutor
      Returns whether this executor has shut down. Effectively, returns whether new tasks will be rejected. This method does not indicate whether all the tasks scheduled have been executed.
      Specified by:
      isShutdown in interface PrioritisedExecutor
      Returns:
      Returns whether this executor has shut down.
    • executeTask

      public boolean executeTask() throws IllegalStateException
      Executes the next available task.

      If there is a task with priority Priority.BLOCKING available, then that such task is executed.

      If there is a task with priority Priority.IDLE available then that task is only executed when there are no other tasks available with a higher priority.

      If there are no tasks that have priority Priority.BLOCKING or Priority.IDLE, then this function will be biased to execute tasks that have higher priorities.

      Specified by:
      executeTask in interface PrioritisedExecutor
      Returns:
      true if a task was executed, false otherwise
      Throws:
      IllegalStateException - Always
    • queueTask

      Description copied from interface: PrioritisedExecutor
      Queues or executes a task at Priority.NORMAL priority.
      Specified by:
      queueTask in interface PrioritisedExecutor
      Parameters:
      task - The task to run.
      Returns:
      null if the current thread immediately executed the task, else returns the prioritised task associated with the parameter
    • queueTask

      public PrioritisedExecutor.PrioritisedTask queueTask(Runnable task, Priority priority)
      Description copied from interface: PrioritisedExecutor
      Queues or executes a task.
      Specified by:
      queueTask in interface PrioritisedExecutor
      Parameters:
      task - The task to run.
      priority - The priority for the task.
      Returns:
      null if the current thread immediately executed the task, else returns the prioritised task associated with the parameter
    • queueTask

      public PrioritisedExecutor.PrioritisedTask queueTask(Runnable task, Priority priority, long subOrder)
      Description copied from interface: PrioritisedExecutor
      Queues or executes a task.
      Specified by:
      queueTask in interface PrioritisedExecutor
      Parameters:
      task - The task to run.
      priority - The priority for the task.
      subOrder - The task's suborder.
      Returns:
      null if the current thread immediately executed the task, else returns the prioritised task associated with the parameter
    • createTask

      Description copied from interface: PrioritisedExecutor
      Creates, but does not queue or execute, a task at Priority.NORMAL priority.
      Specified by:
      createTask in interface PrioritisedExecutor
      Parameters:
      task - The task to run.
      Returns:
      null if the current thread immediately executed the task, else returns the prioritised task associated with the parameter
    • createTask

      public PrioritisedExecutor.PrioritisedTask createTask(Runnable task, Priority priority)
      Description copied from interface: PrioritisedExecutor
      Creates, but does not queue or execute, a task at Priority.NORMAL priority.
      Specified by:
      createTask in interface PrioritisedExecutor
      Parameters:
      task - The task to run.
      priority - The priority for the task.
      Returns:
      null if the current thread immediately executed the task, else returns the prioritised task associated with the parameter
    • createTask

      public PrioritisedExecutor.PrioritisedTask createTask(Runnable task, Priority priority, long subOrder)
      Description copied from interface: PrioritisedExecutor
      Creates, but does not queue or execute, a task at Priority.NORMAL priority.
      Specified by:
      createTask in interface PrioritisedExecutor
      Parameters:
      task - The task to run.
      priority - The priority for the task.
      subOrder - The task's suborder.
      Returns:
      null if the current thread immediately executed the task, else returns the prioritised task associated with the parameter
    • close

      public boolean close(boolean wait, boolean killQueue)
      Closes this queue executor's queue. Optionally waits for all tasks in queue to be executed if wait is true.

      This function is MT-Safe.

      Parameters:
      wait - If this call is to wait until this thread shuts down.
      killQueue - Whether to shutdown this thread's queue
      Returns:
      whether this thread shut down the queue
      See Also:
    • halt

      public void halt(boolean killQueue)
      Causes this thread to exit without draining the queue. To ensure tasks are completed, use close(boolean, boolean).

      This is not safe to call with close(boolean, boolean) if wait = true, in which case the waiting thread may block indefinitely.

      This function is MT-Safe.

      Parameters:
      killQueue - Whether to shutdown this thread's queue
      See Also:
    • getThreadParkedVolatile

      protected final boolean getThreadParkedVolatile()
    • exchangeThreadParkedVolatile

      protected final boolean exchangeThreadParkedVolatile(boolean value)
    • setThreadParkedVolatile

      protected final void setThreadParkedVolatile(boolean value)