@ThreadSafe public class MailboxImpl extends Object implements Mailbox
Mailbox
inspired by ArrayBlockingQueue
and tailored towards
our use case with multiple writers, single reader and volatile reads instead of lock & read on count
.Constructor and Description |
---|
MailboxImpl() |
MailboxImpl(int capacityPow2) |
Modifier and Type | Method and Description |
---|---|
void |
clearAndPut(Runnable shutdownAction)
The effect of this is that all pending letters are dropped and the given priorityAction
is enqueued to the head of the mailbox.
|
boolean |
hasMail()
Returns
true if the mailbox contains mail. |
void |
putMail(Runnable letter)
Enqueues the given letter to the mailbox and blocks until there is capacity for a successful put.
|
Runnable |
takeMail()
This method returns the oldest letter from the mailbox (head of queue) or blocks until a letter is available.
|
boolean |
tryPutMail(Runnable letter)
Enqueues the given letter to the mailbox, if capacity is available.
|
Optional<Runnable> |
tryTakeMail()
Returns an optional with either the oldest letter from the mailbox (head of queue) if the mailbox is not empty or
an empty optional otherwise.
|
void |
waitUntilHasCapacity()
This method blocks until the mailbox has again capacity to enqueue new letters.
|
void |
waitUntilHasMail()
This method blocks if the mailbox is empty until mail becomes available.
|
public MailboxImpl()
public MailboxImpl(int capacityPow2)
public boolean hasMail()
MailboxReceiver
true
if the mailbox contains mail.hasMail
in interface MailboxReceiver
public Optional<Runnable> tryTakeMail()
MailboxReceiver
tryTakeMail
in interface MailboxReceiver
@Nonnull public Runnable takeMail() throws InterruptedException
MailboxReceiver
takeMail
in interface MailboxReceiver
InterruptedException
- on interruption.public void waitUntilHasMail() throws InterruptedException
MailboxReceiver
waitUntilHasMail
in interface MailboxReceiver
InterruptedException
- on interruption.public boolean tryPutMail(@Nonnull Runnable letter)
MailboxSender
true
and false
if the mailbox was already full.tryPutMail
in interface MailboxSender
letter
- the letter to enqueue.true
iff successful.public void putMail(@Nonnull Runnable letter) throws InterruptedException
MailboxSender
putMail
in interface MailboxSender
letter
- the letter to enqueue.InterruptedException
- on interruption.public void waitUntilHasCapacity() throws InterruptedException
MailboxSender
waitUntilHasCapacity
in interface MailboxSender
InterruptedException
- on interruption.public void clearAndPut(@Nonnull Runnable shutdownAction)
Mailbox
clearAndPut
in interface Mailbox
shutdownAction
- action to enqueue atomically after the mailbox was cleared.Copyright © 2014–2020 The Apache Software Foundation. All rights reserved.