Class PrioritisedQueueExecutorThread
- All Implemented Interfaces:
PrioritisedExecutor,Runnable
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.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandlerNested classes/interfaces inherited from interface ca.spottedleaf.concurrentutil.executor.PrioritisedExecutor
PrioritisedExecutor.PrioritisedTask, PrioritisedExecutor.PriorityState -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final longprotected booleanprotected final PrioritisedExecutorprotected final longprotected static final VarHandleprotected booleanprotected booleanFields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY -
Constructor Summary
ConstructorsConstructorDescriptionPrioritisedQueueExecutorThread(PrioritisedExecutor queue, long spinWaitTimeNS) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidbegin()booleanclose(boolean wait, boolean killQueue) Closes this queue executor's queue.protected final booleancompareAndExchangeThreadParkedVolatile(boolean expect, boolean update) createTask(Runnable task) Creates, but does not queue or execute, a task atPriority.NORMALpriority.createTask(Runnable task, Priority priority) Creates, but does not queue or execute, a task atPriority.NORMALpriority.createTask(Runnable task, Priority priority, long subOrder, long stream) Creates, but does not queue or execute, a task atPriority.NORMALpriority.protected voiddie()booleanExecutes the next available task.longGenerates the next suborder id.protected final booleanlongReturns the number of tasks that have been executed.longReturns the number of tasks that have been scheduled are pending to be scheduled.voidhalt(boolean killQueue) Causes this thread to exit without draining the queue.protected final booleanbooleanReturns whether this executor has shut down.final booleanNotify this thread that a task has been added to its queueprotected booleanAttempts to poll as many tasks as possible, returning when finished.Queues or executes a task atPriority.NORMALpriority.Queues or executes a task.Queues or executes a task.final voidrun()protected final voidsetThreadParkedVolatile(boolean value) booleanshutdown()Prevent further additions to this executor.Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, start, startVirtualThread, stop, suspend, threadId, toString, yield
-
Field Details
-
queue
-
threadShutdown
protected volatile boolean threadShutdown -
threadParked
protected volatile boolean threadParked -
THREAD_PARKED_HANDLE
-
halted
protected volatile boolean halted -
spinWaitTimeNS
protected final long spinWaitTimeNS -
DEFAULT_SPINWAIT_TIME
protected static final long DEFAULT_SPINWAIT_TIME- See Also:
-
-
Constructor Details
-
PrioritisedQueueExecutorThread
-
PrioritisedQueueExecutorThread
-
-
Method Details
-
run
public final void run() -
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 final boolean handleClose() -
notifyTasks
public final boolean notifyTasks()Notify this thread that a task has been added to its queue- Returns:
trueif this thread was waiting for tasks,falseif it is executing tasks
-
getTotalTasksExecuted
public long getTotalTasksExecuted()Description copied from interface:PrioritisedExecutorReturns the number of tasks that have been executed.- Specified by:
getTotalTasksExecutedin interfacePrioritisedExecutor
-
getTotalTasksScheduled
public long getTotalTasksScheduled()Description copied from interface:PrioritisedExecutorReturns the number of tasks that have been scheduled are pending to be scheduled.- Specified by:
getTotalTasksScheduledin interfacePrioritisedExecutor
-
generateNextSubOrder
public long generateNextSubOrder()Description copied from interface:PrioritisedExecutorGenerates the next suborder id.- Specified by:
generateNextSubOrderin interfacePrioritisedExecutor- Returns:
- The next suborder id.
-
shutdown
public boolean shutdown()Description copied from interface:PrioritisedExecutorPrevent further additions to this executor. Attempts to add after this call has completed (potentially during) will result inIllegalStateExceptionbeing 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:
shutdownin interfacePrioritisedExecutor- Returns:
trueif the executor was shutdown,falseif it has shut down already- See Also:
-
isShutdown
public boolean isShutdown()Description copied from interface:PrioritisedExecutorReturns 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:
isShutdownin interfacePrioritisedExecutor- Returns:
- Returns whether this executor has shut down.
-
executeTask
Executes the next available task.If there is a task with priority
Priority.BLOCKINGavailable, then that such task is executed.If there is a task with priority
Priority.IDLEavailable 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.BLOCKINGorPriority.IDLE, then this function will be biased to execute tasks that have higher priorities.- Specified by:
executeTaskin interfacePrioritisedExecutor- Returns:
trueif a task was executed,falseotherwise- Throws:
IllegalStateException- Always
-
queueTask
Description copied from interface:PrioritisedExecutorQueues or executes a task atPriority.NORMALpriority.- Specified by:
queueTaskin interfacePrioritisedExecutor- Parameters:
task- The task to run.- Returns:
nullif the current thread immediately executed the task, else returns the prioritised task associated with the parameter
-
queueTask
Description copied from interface:PrioritisedExecutorQueues or executes a task.- Specified by:
queueTaskin interfacePrioritisedExecutor- Parameters:
task- The task to run.priority- The priority for the task.- Returns:
nullif 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, long stream) Description copied from interface:PrioritisedExecutorQueues or executes a task.- Specified by:
queueTaskin interfacePrioritisedExecutor- Parameters:
task- The task to run.priority- The priority for the task.subOrder- The task's suborder.stream- The task's stream id.- Returns:
nullif the current thread immediately executed the task, else returns the prioritised task associated with the parameter
-
createTask
Description copied from interface:PrioritisedExecutorCreates, but does not queue or execute, a task atPriority.NORMALpriority.- Specified by:
createTaskin interfacePrioritisedExecutor- Parameters:
task- The task to run.- Returns:
nullif the current thread immediately executed the task, else returns the prioritised task associated with the parameter
-
createTask
Description copied from interface:PrioritisedExecutorCreates, but does not queue or execute, a task atPriority.NORMALpriority.- Specified by:
createTaskin interfacePrioritisedExecutor- Parameters:
task- The task to run.priority- The priority for the task.- Returns:
nullif 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, long stream) Description copied from interface:PrioritisedExecutorCreates, but does not queue or execute, a task atPriority.NORMALpriority.- Specified by:
createTaskin interfacePrioritisedExecutor- Parameters:
task- The task to run.priority- The priority for the task.subOrder- The task's suborder.stream- The task's stream.- Returns:
nullif 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 ifwaitis 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, useclose(boolean, boolean).This is not safe to call with
close(boolean, boolean)ifwait = 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() -
compareAndExchangeThreadParkedVolatile
protected final boolean compareAndExchangeThreadParkedVolatile(boolean expect, boolean update) -
setThreadParkedVolatile
protected final void setThreadParkedVolatile(boolean value)
-