@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.State
MAX_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.
|
@VisibleForTesting public TaskMailboxImpl()
public boolean isMailboxThread()
TaskMailbox
Read operations will fail if they are called from another thread.
isMailboxThread
in interface TaskMailbox
public boolean hasMail()
TaskMailbox
true
if the mailbox contains mail.
Must be called from the mailbox thread (TaskMailbox.isMailboxThread()
.
hasMail
in interface TaskMailbox
public Optional<Mail> tryTake(int priority)
TaskMailbox
Must be called from the mailbox thread (TaskMailbox.isMailboxThread()
.
tryTake
in interface TaskMailbox
@Nonnull public Mail take(int priority) throws InterruptedException, IllegalStateException
TaskMailbox
Must be called from the mailbox thread (TaskMailbox.isMailboxThread()
.
take
in interface TaskMailbox
InterruptedException
- on interruption.IllegalStateException
- if mailbox is already closed.public boolean createBatch()
TaskMailbox
TaskMailbox.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 TaskMailbox
public Optional<Mail> tryTakeFromBatch()
TaskMailbox
Must 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 TaskMailbox
public void put(@Nonnull Mail mail)
TaskMailbox
Mails can be added from any thread.
put
in interface TaskMailbox
mail
- the mail to enqueue.public void putFirst(@Nonnull Mail mail)
TaskMailbox
Mails can be added from any thread.
putFirst
in interface TaskMailbox
mail
- the mail to enqueue.public List<Mail> drain()
TaskMailbox
drain
in interface TaskMailbox
public void quiesce()
TaskMailbox
IllegalStateException
.quiesce
in interface TaskMailbox
@Nonnull public List<Mail> close()
TaskMailbox
IllegalStateException
. Returns all mails that were still enqueued.close
in interface TaskMailbox
@Nonnull public TaskMailbox.State getState()
TaskMailbox
TaskMailbox.State
.getState
in interface TaskMailbox
public void runExclusively(Runnable runnable)
TaskMailbox
TaskMailbox.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 TaskMailbox
runnable
- the runnable to executeCopyright © 2014–2020 The Apache Software Foundation. All rights reserved.