Class ExecutionQueue
- java.lang.Object
-
- com.inductiveautomation.ignition.common.util.ExecutionQueue
-
public class ExecutionQueue extends java.lang.Object
Queues up submittedRunnable
s and executes them in serial on anExecutorService
.
-
-
Constructor Summary
Constructors Constructor Description ExecutionQueue(java.util.concurrent.Executor service)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
isInQueue()
void
pause()
Pause execution of queuedRunnable
s.void
requireInQueue()
Call to check if the current code is being executed inside this queue.void
requireInQueue(java.lang.String message)
Call to check if the current code is being executed inside this queue.void
resume()
Resume execution of queuedRunnable
s.<E extends java.lang.Throwable>
java.util.concurrent.CompletableFuture<java.lang.Void>runOrSubmit(FragileRunnable<E> fragileRunnable)
Runs the givenFragileRunnable
on the calling thread ifisInQueue()
is true.<T,E extends java.lang.Throwable>
java.util.concurrent.CompletableFuture<T>runOrSubmit(FragileSupplier<T,E> fragileSupplier)
Runs the givenFragileSupplier
on the calling thread ifisInQueue()
is true.void
submit(java.lang.Runnable runnable)
Submit aRunnable
to be executed.void
submit(java.lang.Runnable... runnables)
Submit one or moreRunnable
s to be executed.void
submitOrRun(java.lang.Runnable runnable)
void
submitToHead(java.lang.Runnable runnable)
Submit aRunnable
to be executed at the head of the queue.
-
-
-
Method Detail
-
submit
public void submit(java.lang.Runnable runnable)
Submit aRunnable
to be executed.- Parameters:
runnable
- theRunnable
to be executed.
-
submit
public void submit(java.lang.Runnable... runnables)
Submit one or moreRunnable
s to be executed.- Parameters:
runnables
- theRunnable
s to be executed.
-
submitToHead
public void submitToHead(java.lang.Runnable runnable)
Submit aRunnable
to be executed at the head of the queue.- Parameters:
runnable
- theRunnable
to be executed.
-
submitOrRun
public void submitOrRun(@Nonnull java.lang.Runnable runnable)
- Parameters:
runnable
- theRunnable
to run on-queue.
-
runOrSubmit
@Nonnull public <E extends java.lang.Throwable> java.util.concurrent.CompletableFuture<java.lang.Void> runOrSubmit(FragileRunnable<E> fragileRunnable)
Runs the givenFragileRunnable
on the calling thread ifisInQueue()
is true. Queues the givenFragileRunnable
to be run on-queue ifisInQueue()
is false. Useful for cases where the caller wishes to block until the givenFragileRunnable
completes execution but the caller may or may not already be running on-queue. If theFragileRunnable
throws aThrowable
during execution, it will be caught and used to complete the returnedCompletableFuture
exceptionally.- Parameters:
fragileRunnable
- theFragileRunnable
to run on-queue. must not be null.- Returns:
- a
CompletableFuture
which will be completed after the givenFragileRunnable
is ran. it will already be completed when called on a thread whereisInQueue()
is true, else it will be completed once the queue runs the givenFragileRunnable
. - Throws:
java.lang.NullPointerException
- if the givenFragileRunnable
is null
-
runOrSubmit
@Nonnull public <T,E extends java.lang.Throwable> java.util.concurrent.CompletableFuture<T> runOrSubmit(FragileSupplier<T,E> fragileSupplier)
Runs the givenFragileSupplier
on the calling thread ifisInQueue()
is true. Queues the givenFragileSupplier
to be run on-queue ifisInQueue()
is false. Useful for cases where the caller wishes to block until the givenFragileSupplier
completes execution but the caller may or may not already be running on-queue. If theFragileSupplier
throws aThrowable
during execution, it will be caught and used to complete the returnedCompletableFuture
exceptionally.- Parameters:
fragileSupplier
- theFragileSupplier
to run on-queue. must not be null.- Returns:
- a
CompletableFuture
which will be completed after the givenFragileSupplier
is ran with the value returned by callingFragileSupplier.get()
. it will already be completed when called on a thread whereisInQueue()
is true, else it will be completed once the queue runs the givenFragileSupplier
. - Throws:
java.lang.NullPointerException
- if the givenFragileSupplier
is null
-
pause
public void pause()
Pause execution of queuedRunnable
s.
-
resume
public void resume()
Resume execution of queuedRunnable
s.
-
isInQueue
public boolean isInQueue()
- Returns:
true
if this thread is executing inside this queue.
-
requireInQueue
public void requireInQueue()
Call to check if the current code is being executed inside this queue.- Throws:
java.lang.IllegalStateException
- if flow reaches here without going through this queue
-
requireInQueue
public void requireInQueue(java.lang.String message)
Call to check if the current code is being executed inside this queue.- Parameters:
message
- The message to use in the thrown exception if the check fails- Throws:
java.lang.IllegalStateException
- if flow reaches here without going through this queue
-
-