@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.StateMAX_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()
TaskMailboxRead operations will fail if they are called from another thread.
isMailboxThread 在接口中 TaskMailboxpublic boolean hasMail()
TaskMailboxtrue if the mailbox contains mail.
Must be called from the mailbox thread (TaskMailbox.isMailboxThread().
hasMail 在接口中 TaskMailboxpublic int size()
TaskMailboxsize 在接口中 TaskMailboxpublic Optional<Mail> tryTake(int priority)
TaskMailboxMust be called from the mailbox thread (TaskMailbox.isMailboxThread().
tryTake 在接口中 TaskMailbox@Nonnull public Mail take(int priority) throws InterruptedException, IllegalStateException
TaskMailboxMust be called from the mailbox thread (TaskMailbox.isMailboxThread().
take 在接口中 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 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 在接口中 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 在接口中 TaskMailboxpublic void put(@Nonnull Mail mail)
TaskMailboxMails can be added from any thread.
put 在接口中 TaskMailboxmail - the mail to enqueue.public void putFirst(@Nonnull Mail mail)
TaskMailboxMails can be added from any thread.
putFirst 在接口中 TaskMailboxmail - the mail to enqueue.public List<Mail> drain()
TaskMailboxdrain 在接口中 TaskMailboxpublic void quiesce()
TaskMailboxTaskMailbox.MailboxClosedException.quiesce 在接口中 TaskMailbox@Nonnull public List<Mail> close()
TaskMailboxTaskMailbox.MailboxClosedException. Returns all mails that were
still enqueued.close 在接口中 TaskMailbox@Nonnull public TaskMailbox.State getState()
TaskMailboxTaskMailbox.State.getState 在接口中 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 在接口中 TaskMailboxrunnable - the runnable to executeCopyright © 2014–2022 The Apache Software Foundation. All rights reserved.