E
- Type of items in the Qpublic class SingleConsumerQueue<E> extends Object
This is a Wrapper over a LinkedBlockingQueue
for use with a Single Consumer. This is used by the internal request and
response Q's of AsyncRedisConnection
. The Consumer Thread can optionally quit on an empty Q in which case
the Q is marked as IDLE. The writer has an option to acquire the Q if it is IDLE when writing to the head of the Q.
If the Q is acquired then then the writer should initiate the listener.
Note: Only One consumer should be receiving for a SingleConsumerQueue. This can be ensured by using add method to acquire an idle consumer status and initiate the consumer and the remove method to set back the consumer status to idle and quit the consumer thread in case of an empty Q.
Modifier and Type | Class and Description |
---|---|
static interface |
SingleConsumerQueue.IdleListener<E>
Callback Interface to get called back when the IDLE status of the Q changes.
|
static class |
SingleConsumerQueue.QueueStatus |
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_MAX_ENQUE_TIME_MILLIS
Default value of maxEnqueTimeMillis.
|
Constructor and Description |
---|
SingleConsumerQueue()
Creates a Q with unbounded capacity.
|
SingleConsumerQueue(int pcapacity)
Creates a Q with the given capacity and default maxEnqueTimeMillis of 5 seconds
|
SingleConsumerQueue(int pcapacity,
long pmaxEnqueTimeMillis)
Creates a Q with the given capacity and maxEnqueTimeMillis to wait for when adding to the Q in case the Q is
full.
|
Modifier and Type | Method and Description |
---|---|
boolean |
acquireIdle()
Acquires an idle Q and marks it as active.
|
boolean |
add(E e,
boolean acquireIfIdle)
Adds an item to the head of the Q.
|
SingleConsumerQueue.IdleListener<E> |
getIdleListener()
Gets the idle listener in use.
|
void |
markIdle()
Unconditionally mark the Q idle.
|
void |
push(E e)
Push to head of Q.
|
E |
remove(long timeoutMicros,
boolean releaseIfEmpty)
Remove from the tail of the Q.
|
void |
setIdleListener(SingleConsumerQueue.IdleListener<E> pidleListener)
Sets an idle listener which is called whenever the Q status changes from IDLE to ACTIVE or vice-versa.
|
int |
size()
Gets the size of the Q.
|
public static final int DEFAULT_MAX_ENQUE_TIME_MILLIS
public SingleConsumerQueue(int pcapacity, long pmaxEnqueTimeMillis)
pcapacity
- Q capacity or 0 for unboundedpmaxEnqueTimeMillis
- Max Milliseconds to wait when adding to Q if the Q is fullpublic SingleConsumerQueue(int pcapacity)
pcapacity
- Q capacity or 0 for unboundedpublic SingleConsumerQueue()
public boolean add(E e, boolean acquireIfIdle)
e
- item to addacquireIfIdle
- pass true to acquire the Q if currently idle. When used concurrently exactly one of the
callers gets the return value as true.public E remove(long timeoutMicros, boolean releaseIfEmpty)
timeoutMicros
- Max microseconds to wait for in case the Q is emptyreleaseIfEmpty
- If true then the Q is marked idle if the Q is empty and this method returns null.public void push(E e)
e
- Itempublic void markIdle()
public boolean acquireIdle()
public int size()
public SingleConsumerQueue.IdleListener<E> getIdleListener()
public void setIdleListener(SingleConsumerQueue.IdleListener<E> pidleListener)
pidleListener
- Idle listener or null to unset the current listenerCopyright © 2013- Suresh Mahalingam. All Rights Reserved.