public class Mailbox<T> extends java.lang.Object implements PauseReason, EventPublisher
| Modifier and Type | Class and Description |
|---|---|
class |
Mailbox.BlockingSubscriber |
| Modifier and Type | Field and Description |
|---|---|
static Event |
messageAvailable |
static int |
MSG_AVAILABLE |
static int |
SPACE_AVAILABLE |
static Event |
spaceAvailble |
static int |
TIMED_OUT |
static Event |
timedOut |
| Constructor and Description |
|---|
Mailbox() |
Mailbox(int initialSize) |
Mailbox(int initialSize,
int maxSize) |
| Modifier and Type | Method and Description |
|---|---|
void |
addMsgAvailableListener(EventSubscriber msgSub) |
void |
addSpaceAvailableListener(EventSubscriber spcSub) |
T |
get() |
T |
get(EventSubscriber eo)
Non-blocking, nonpausing get.
|
T |
get(long timeoutMillis) |
T |
getb()
retrieve a message, blocking the thread indefinitely.
|
T |
getb(long timeoutMillis)
retrieve a msg, and block the Java thread for the time given.
|
T |
getnb()
Get, don't pause or block.
|
boolean |
hasMessage() |
boolean |
hasMessage(Task eo) |
boolean |
hasMessages(int num,
Task eo) |
boolean |
hasSpace() |
boolean |
isValid(Task t)
True if the given task's reason for pausing is still valid.
|
java.lang.Object[] |
messages() |
T |
peek(int idx) |
void |
put(T msg)
put a non-null message in the mailbox, and pause the calling task until the
mailbox has space
|
boolean |
put(T msg,
EventSubscriber eo)
Non-blocking, nonpausing put.
|
boolean |
put(T msg,
int timeoutMillis)
put a non-null message in the mailbox, and pause the calling task for timeoutMillis
if the mailbox is full.
|
void |
putb(T msg) |
boolean |
putb(T msg,
long timeoutMillis)
put a non-null message in the mailbox, and block the calling thread for timeoutMillis
if the mailbox is full
|
boolean |
putnb(T msg)
Attempt to put a message, and return true if successful.
|
T |
remove(int idx) |
void |
removeMsgAvailableListener(EventSubscriber msgSub) |
void |
removeSpaceAvailableListener(EventSubscriber spcSub) |
static int |
select(Mailbox... mboxes)
Takes an array of mailboxes and returns the index of the first mailbox
that has a message.
|
int |
size() |
java.lang.String |
toString() |
void |
untilHasMessage()
Block caller until at least one message is available.
|
void |
untilHasMessages(int num)
Block caller until
num messages are available. |
public static final int SPACE_AVAILABLE
public static final int MSG_AVAILABLE
public static final int TIMED_OUT
public static final Event spaceAvailble
public static final Event messageAvailable
public static final Event timedOut
public Mailbox()
public Mailbox(int initialSize)
public Mailbox(int initialSize,
int maxSize)
public T get(EventSubscriber eo)
eo. - If non-null, registers this observer and calls it with a MessageAvailable event when
a put() is done.public boolean put(T msg, EventSubscriber eo)
eo. - If non-null, registers this observer and calls it with an SpaceAvailable event
when there's space.putnb(Object),
putb(Object)public T getnb()
public T get(long timeoutMillis) throws Pausable
Pausablepublic void untilHasMessage()
throws Pausable
Pausablepublic void untilHasMessages(int num)
throws Pausable
num messages are available.num - Pausablepublic boolean hasMessage(Task eo)
public boolean hasMessages(int num,
Task eo)
public T peek(int idx)
public T remove(int idx)
public java.lang.Object[] messages()
public static int select(Mailbox... mboxes) throws Pausable
Pausablepublic void addSpaceAvailableListener(EventSubscriber spcSub)
public void removeSpaceAvailableListener(EventSubscriber spcSub)
public void addMsgAvailableListener(EventSubscriber msgSub)
public void removeMsgAvailableListener(EventSubscriber msgSub)
public boolean putnb(T msg)
public void put(T msg) throws Pausable
Pausablepublic boolean put(T msg, int timeoutMillis) throws Pausable
Pausablepublic void putb(T msg)
public boolean putb(T msg, long timeoutMillis)
public int size()
public boolean hasMessage()
public boolean hasSpace()
public T getb()
public T getb(long timeoutMillis)
millis. - max wait timepublic java.lang.String toString()
toString in class java.lang.Objectpublic boolean isValid(Task t)
PauseReasonisValid in interface PauseReason