java.lang.Object
com.renomad.minum.queue.ActionQueue
- All Implemented Interfaces:
AbstractActionQueue
This class provides the ability to pop items into
a queue thread-safely and know they'll happen later.
For example, this is helpful for minum.logging, or passing functions to a minum.database. It lets us run a bit faster, since the I/O actions are happening on a separate thread and the only time required is passing the function of what we want to run later.
Example:
This example shows where an InputStream representing the bytes
of an image file are sent to the ActionQueue for processing. The call
to enqueue(String, ThrowingRunnable) returns immediately,
and processing continues on another thread.
ActionQueue photoResizingQueue;
InputStream photoInputStream;
photoResizingQueue = new ActionQueue("photo_resizing", context).initialize();
photoResizingQueue.enqueue("resize an image", () -> resizeImage(photoInputStream));
-
Constructor Summary
ConstructorsConstructorDescriptionActionQueue(String name, Context context) See theActionQueuedescription for more detail. -
Method Summary
Modifier and TypeMethodDescriptionvoidenqueue(String description, ThrowingRunnable action) Adds something to the queue to be processed.getQueue()Get theQueueof data that is supposed to get processed.Start the queue's processingbooleanIndicate whether this has had itsAbstractActionQueue.stop()method completed.voidstop()This will prevent any new actions being queued (by setting the stop flag to true and thus causing an exception to be thrown when a call is made to [enqueue]) and will block until the queue is empty.voidstop(int count, int sleepTime) Stops the action queuetoString()
-
Constructor Details
-
ActionQueue
See theActionQueuedescription for more detail. This constructor will build your new action queue and handle registering it with a list of other action queues in theContextobject.- Parameters:
name- give this object a unique, explanatory name.
-
-
Method Details
-
initialize
Description copied from interface:AbstractActionQueueStart the queue's processing- Specified by:
initializein interfaceAbstractActionQueue
-
enqueue
Adds something to the queue to be processed.Here is an example use of .enqueue:
actionQueue.enqueue("Write person file to disk at " + filePath, () -> { Files.writeString(filePath, pf.serialize()); });- Specified by:
enqueuein interfaceAbstractActionQueue
-
stop
public void stop(int count, int sleepTime) Stops the action queue- Specified by:
stopin interfaceAbstractActionQueue- Parameters:
count- how many loops to wait before we crash it closedsleepTime- how long to wait in milliseconds between loops
-
stop
public void stop()This will prevent any new actions being queued (by setting the stop flag to true and thus causing an exception to be thrown when a call is made to [enqueue]) and will block until the queue is empty.- Specified by:
stopin interfaceAbstractActionQueue
-
toString
-
getQueue
Description copied from interface:AbstractActionQueueGet theQueueof data that is supposed to get processed.- Specified by:
getQueuein interfaceAbstractActionQueue
-
isStopped
public boolean isStopped()Description copied from interface:AbstractActionQueueIndicate whether this has had itsAbstractActionQueue.stop()method completed.- Specified by:
isStoppedin interfaceAbstractActionQueue
-