@ThreadSafe public class TaskMailboxImpl extends Object implements TaskMailbox
TaskMailbox in a BlockingQueue fashion and tailored towards
our use case with multiple writers and single reader.TaskMailbox.StateMAX_PRIORITY, MIN_PRIORITY| Constructor and Description |
|---|
TaskMailboxImpl() |
TaskMailboxImpl(Thread taskMailboxThread) |
| Modifier and Type | Method and Description |
|---|---|
List<Mail> |
close()
Close the mailbox.
|
boolean |
createBatch()
Creates a batch of mails that can be taken with
TaskMailbox.tryTakeFromBatch(). |
List<Mail> |
drain()
Drains the mailbox and returns all mails that were still enqueued.
|
TaskMailbox.State |
getState()
Returns the current state of the mailbox as defined by the lifecycle enum
TaskMailbox.State. |
boolean |
hasMail()
Returns
true if the mailbox contains mail. |
boolean |
isMailboxThread()
Check if the current thread is the mailbox thread.
|
void |
put(Mail mail)
Enqueues the given mail to the mailbox and blocks until there is capacity for a successful put.
|
void |
putFirst(Mail mail)
Adds the given action to the head of the mailbox.
|
void |
quiesce()
Quiesce the mailbox.
|
void |
runExclusively(Runnable runnable)
Runs the given code exclusively on this mailbox.
|
Mail |
take(int priority)
This method returns the oldest mail from the mailbox (head of queue) or blocks until a mail is available.
|
Optional<Mail> |
tryTake(int priority)
Returns an optional with either the oldest mail from the mailbox (head of queue) if the mailbox is not empty or
an empty optional otherwise.
|
Optional<Mail> |
tryTakeFromBatch()
Returns an optional with either the oldest mail from the batch (head of queue) if the batch is not empty or an
empty optional otherwise.
|
public boolean isMailboxThread()
TaskMailboxRead operations will fail if they are called from another thread.
isMailboxThread in interface TaskMailboxpublic boolean hasMail()
TaskMailboxtrue if the mailbox contains mail.
Must be called from the mailbox thread (TaskMailbox.isMailboxThread().
hasMail in interface TaskMailboxpublic Optional<Mail> tryTake(int priority)
TaskMailboxMust be called from the mailbox thread (TaskMailbox.isMailboxThread().
tryTake in interface TaskMailbox@Nonnull public Mail take(int priority) throws InterruptedException, IllegalStateException
TaskMailboxMust be called from the mailbox thread (TaskMailbox.isMailboxThread().
take in interface TaskMailboxInterruptedException - on interruption.IllegalStateException - if mailbox is already closed.public boolean createBatch()
TaskMailboxTaskMailbox.tryTakeFromBatch(). The batch does not affect
TaskMailbox.tryTake(int) and TaskMailbox.take(int); that is, they return the same mails even if no batch would have
been created.
The default batch is empty. Thus, this method must be invoked once before TaskMailbox.tryTakeFromBatch().
If a batch is not completely consumed by TaskMailbox.tryTakeFromBatch(), its elements are carried over to the
new batch.
Must be called from the mailbox thread (TaskMailbox.isMailboxThread().
createBatch in interface TaskMailboxpublic Optional<Mail> tryTakeFromBatch()
TaskMailboxMust be called from the mailbox thread (TaskMailbox.isMailboxThread().
Note that there is no blocking takeFromBatch as batches can only be created and consumed from the
mailbox thread.
tryTakeFromBatch in interface TaskMailboxpublic void put(@Nonnull Mail mail)
TaskMailboxMails can be added from any thread.
put in interface TaskMailboxmail - the mail to enqueue.public void putFirst(@Nonnull Mail mail)
TaskMailboxMails can be added from any thread.
putFirst in interface TaskMailboxmail - the mail to enqueue.public List<Mail> drain()
TaskMailboxdrain in interface TaskMailboxpublic void quiesce()
TaskMailboxIllegalStateException.quiesce in interface TaskMailbox@Nonnull public List<Mail> close()
TaskMailboxIllegalStateException. Returns all mails that were still enqueued.close in interface TaskMailbox@Nonnull public TaskMailbox.State getState()
TaskMailboxTaskMailbox.State.getState in interface TaskMailboxpublic void runExclusively(Runnable runnable)
TaskMailboxTaskMailbox.put(Mail) or modifying lifecycle methods).
Use this methods when you want to atomically execute code that uses different methods (e.g., check for state and then put message if open).
runExclusively in interface TaskMailboxrunnable - the runnable to executeCopyright © 2014–2020 The Apache Software Foundation. All rights reserved.