Package kilim
Class Cell<T>
- java.lang.Object
-
- kilim.Cell<T>
-
- All Implemented Interfaces:
EventPublisher,PauseReason
public class Cell<T> extends java.lang.Object implements PauseReason, EventPublisher
A cell is a single-space buffer that supports multiple producers and a single consumer, functionally identical to Mailbox bounded to a size of 1 (and hence optimized for this size)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classCell.BlockingSubscriber
-
Field Summary
Fields Modifier and Type Field Description (package private) VolatileReferenceCell<T>messagestatic EventmessageAvailablestatic intMSG_AVAILABLE(package private) VolatileReferenceCell<EventSubscriber>sinkstatic intSPACE_AVAILABLEstatic EventspaceAvailble(package private) java.util.Queue<EventSubscriber>srcsstatic intTIMED_OUTstatic EventtimedOut
-
Constructor Summary
Constructors Constructor Description Cell()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddMsgAvailableListener(EventSubscriber msgSub)voidaddSpaceAvailableListener(EventSubscriber spcSub)Tget()Tget(long timeoutMillis)Tget(EventSubscriber eo)Non-blocking, nonpausing get.Tgetb()retrieve a message, blocking the thread indefinitely.Tgetb(long timeoutMillis)retrieve a msg, and block the Java thread for the time given.Tgetnb()Get, don't pause or block.booleanhasMessage()booleanhasSpace()booleanisValid(Task t)True if the given task's reason for pausing is still valid.voidput(T msg)booleanput(T msg, int timeoutMillis)booleanput(T msg, EventSubscriber eo)Non-blocking, nonpausing put.voidputb(T msg)voidputb(T msg, long timeoutMillis)booleanputnb(T msg)voidremoveMsgAvailableListener(EventSubscriber msgSub)voidremoveSpaceAvailableListener(EventSubscriber spcSub)java.lang.StringtoString()
-
-
-
Field Detail
-
srcs
java.util.Queue<EventSubscriber> srcs
-
SPACE_AVAILABLE
public static final int SPACE_AVAILABLE
- See Also:
- Constant Field Values
-
MSG_AVAILABLE
public static final int MSG_AVAILABLE
- See Also:
- Constant Field Values
-
TIMED_OUT
public static final int TIMED_OUT
- See Also:
- Constant Field Values
-
spaceAvailble
public static final Event spaceAvailble
-
messageAvailable
public static final Event messageAvailable
-
timedOut
public static final Event timedOut
-
sink
VolatileReferenceCell<EventSubscriber> sink
-
message
VolatileReferenceCell<T> message
-
-
Method Detail
-
get
public T get(EventSubscriber eo)
Non-blocking, nonpausing get.- Parameters:
eo- . If non-null (and if there is no message), registers this observer. The observer is notified with a MessageAvailable event when a put() is done.- Returns:
- buffered message if there's one, or null
-
put
public boolean put(T msg, EventSubscriber eo)
Non-blocking, nonpausing put.- Parameters:
eo- . If non-null, registers this observer and calls it with an SpaceAvailable event when there's space.- Returns:
- buffered message if there's one, or null
-
getnb
public T getnb()
Get, don't pause or block.- Returns:
- stored message, or null if no message found.
-
addSpaceAvailableListener
public void addSpaceAvailableListener(EventSubscriber spcSub)
-
removeSpaceAvailableListener
public void removeSpaceAvailableListener(EventSubscriber spcSub)
-
addMsgAvailableListener
public void addMsgAvailableListener(EventSubscriber msgSub)
-
removeMsgAvailableListener
public void removeMsgAvailableListener(EventSubscriber msgSub)
-
putnb
public boolean putnb(T msg)
-
putb
public void putb(T msg)
-
putb
public void putb(T msg, long timeoutMillis)
-
hasMessage
public boolean hasMessage()
-
hasSpace
public boolean hasSpace()
-
getb
public T getb()
retrieve a message, blocking the thread indefinitely. Note, this is a heavyweight block, unlike #get() that pauses the Fiber but doesn't block the thread.
-
getb
public T getb(long timeoutMillis)
retrieve a msg, and block the Java thread for the time given.- Parameters:
millis- . max wait time- Returns:
- null if timed out.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
isValid
public boolean isValid(Task t)
Description copied from interface:PauseReasonTrue if the given task's reason for pausing is still valid.- Specified by:
isValidin interfacePauseReason
-
-