@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.MailboxClosedException, TaskMailbox.State
MAX_PRIORITY, MIN_PRIORITY
构造器和说明 |
---|
TaskMailboxImpl() |
TaskMailboxImpl(Thread taskMailboxThread) |
限定符和类型 | 方法和说明 |
---|---|
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.
|
int |
size()
Returns the current number of mails in 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()
TaskMailbox
Read operations will fail if they are called from another thread.
isMailboxThread
在接口中 TaskMailbox
public boolean hasMail()
TaskMailbox
true
if the mailbox contains mail.
Must be called from the mailbox thread (TaskMailbox.isMailboxThread()
.
hasMail
在接口中 TaskMailbox
public int size()
TaskMailbox
size
在接口中 TaskMailbox
public Optional<Mail> tryTake(int priority)
TaskMailbox
Must be called from the mailbox thread (TaskMailbox.isMailboxThread()
.
tryTake
在接口中 TaskMailbox
@Nonnull public Mail take(int priority) throws InterruptedException, IllegalStateException
TaskMailbox
Must be called from the mailbox thread (TaskMailbox.isMailboxThread()
.
take
在接口中 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 had 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
在接口中 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
在接口中 TaskMailbox
public void put(@Nonnull Mail mail)
TaskMailbox
Mails can be added from any thread.
put
在接口中 TaskMailbox
mail
- the mail to enqueue.public void putFirst(@Nonnull Mail mail)
TaskMailbox
Mails can be added from any thread.
putFirst
在接口中 TaskMailbox
mail
- the mail to enqueue.public List<Mail> drain()
TaskMailbox
drain
在接口中 TaskMailbox
public void quiesce()
TaskMailbox
TaskMailbox.MailboxClosedException
.quiesce
在接口中 TaskMailbox
@Nonnull public List<Mail> close()
TaskMailbox
TaskMailbox.MailboxClosedException
. Returns all mails that were
still enqueued.close
在接口中 TaskMailbox
@Nonnull public TaskMailbox.State getState()
TaskMailbox
TaskMailbox.State
.getState
在接口中 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
在接口中 TaskMailbox
runnable
- the runnable to executeCopyright © 2014–2022 The Apache Software Foundation. All rights reserved.