|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.eigenbase.runtime.QueueIterator
public class QueueIterator
Adapter that exposes a 'push' producer as an Iterator
. Supports one
or more producers feeding into a single consumer. The consumer and the
producers must each run in its own thread. When there are several producers
the data is merged as it arrives: no sorting.
By default, the queue contains at most one object (implemented via SynchronousQueue
), but this can be customized by supplying an alternate
implementation (e.g. ArrayBlockingQueue
) to the constructor. If you
call next
, your thread will wait until a producer thread calls
put(java.lang.Object)
or done(java.lang.Throwable)
. Nulls are allowed. If a producer has an error,
it can pass it to the consumer via done(java.lang.Throwable)
.
Nested Class Summary | |
---|---|
private static class |
QueueIterator.EndOfQueue
Sentinel object. |
static class |
QueueIterator.TimeoutException
Thrown by hasNext(long) and next(long) to indicate that operation timed out before
rows were available. |
private static class |
QueueIterator.WrappedNull
A null masquerading as a real object. |
Field Summary | |
---|---|
protected boolean |
hasNext
false when Iterator is finished |
protected Object |
next
next Iterator value (nulls are represented via #WRAPPED_NULL) |
private int |
numProducers
|
protected BlockingQueue |
queue
|
protected Throwable |
throwable
|
private EigenbaseLogger |
tracer
|
private static QueueIterator.WrappedNull |
WRAPPED_NULL
|
Constructor Summary | |
---|---|
QueueIterator()
default constructor (one producer, no tracer, SynchronousQueue) |
|
QueueIterator(int n,
Logger tracer)
|
|
QueueIterator(int n,
Logger tracer,
BlockingQueue queue)
|
Method Summary | |
---|---|
protected void |
checkTermination()
Checks for end-of-queue, and throws an error if one has been set via done(Throwable) . |
void |
done(Throwable throwable)
Producer calls done to say that there are no more objects,
setting throwable if there was an error. |
boolean |
hasNext()
|
boolean |
hasNext(long timeoutMillis)
As hasNext , but throws QueueIterator.TimeoutException if no row is
available within the timeout. |
Object |
next()
|
Object |
next(long timeoutMillis)
As next , but throws QueueIterator.TimeoutException if no row is
available within the timeout. |
boolean |
offer(Object o,
long timeoutMillis)
Producer calls offer to attempt to add another object (which
may be null) with a timeout. |
protected void |
onEndOfQueue()
Called (from the consumer thread context) just before the iterator returns false for hasNext(). |
void |
put(Object o)
Producer calls put to add another object (which may be
null). |
void |
remove()
|
protected void |
reset(int n)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final QueueIterator.WrappedNull WRAPPED_NULL
private int numProducers
private final EigenbaseLogger tracer
protected Object next
protected boolean hasNext
protected Throwable throwable
protected BlockingQueue queue
Constructor Detail |
---|
public QueueIterator()
public QueueIterator(int n, Logger tracer)
n
- number of producerstracer
- trace to this Logger, or null.public QueueIterator(int n, Logger tracer, BlockingQueue queue)
n
- number of producerstracer
- trace to this Logger, or null.queue
- BlockingQueue
implementation, or null for defaultMethod Detail |
---|
protected void reset(int n)
public void done(Throwable throwable)
done
to say that there are no more objects,
setting throwable
if there was an error.
public boolean hasNext()
hasNext
in interface Iterator
public boolean hasNext(long timeoutMillis) throws QueueIterator.TimeoutException
hasNext
, but throws QueueIterator.TimeoutException
if no row is
available within the timeout.
timeoutMillis
- Milliseconds to wait; less than or equal to zero
means don't wait
QueueIterator.TimeoutException
public Object next()
next
in interface Iterator
public Object next(long timeoutMillis) throws QueueIterator.TimeoutException
next
, but throws QueueIterator.TimeoutException
if no row is
available within the timeout.
timeoutMillis
- Milliseconds to wait; less than or equal to zero
means don't wait
QueueIterator.TimeoutException
public void put(Object o)
put
to add another object (which may be
null).
o
- object to put
IllegalStateException
- if this method is called after done(java.lang.Throwable)
public boolean offer(Object o, long timeoutMillis)
offer
to attempt to add another object (which
may be null) with a timeout.
o
- object to offertimeoutMillis
- Milliseconds to wait; less than or equal to zero
means don't wait
IllegalStateException
- if this method is called after done(java.lang.Throwable)
public void remove()
remove
in interface Iterator
protected void checkTermination()
done(Throwable)
.
protected void onEndOfQueue()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |