org.eigenbase.runtime
Class ThreadIterator
java.lang.Object
org.eigenbase.runtime.QueueIterator
org.eigenbase.runtime.ThreadIterator
- All Implemented Interfaces:
- Runnable, Iterator, Iterable
- Direct Known Subclasses:
- FarragoJavaUdxIterator, ThreadIteratorTest.ArrayIterator
public abstract class ThreadIterator
- extends QueueIterator
- implements Iterator, Runnable, Iterable
ThreadIterator
converts 'push' code to 'pull'. You implement
doWork()
to call QueueIterator.put(java.lang.Object)
with each row, and this class invokes it
in a separate thread. Then the results come out via the familiar Iterator
interface. For example,
class ArrayIterator extends ThreadIterator {
Object[] a;
ArrayIterator(Object[] a) {
this.a = a;
start();
}
protected void doWork() {
for (int i = 0; i < a.length; i++) {
put(a[i]);
}
}
}
Or, more typically, using an anonymous class:
Iterator i = new ThreadIterator() {
int limit;
public ThreadIterator start(int limit) {
this.limit = limit;
return super.start();
}
protected void doWork() {
for (int i = 0; i < limit; i++) {
put(new Integer(i));
}
}
}.start(100);
while (i.hasNext()) {
etc.
}
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
thread
private Thread thread
ThreadIterator
public ThreadIterator()
ThreadIterator
public ThreadIterator(BlockingQueue queue)
iterator
public Iterator iterator()
- Description copied from interface:
Iterable
- Returns an iterator over the elements in this collection. There are no
guarantees over the order in which the elements are returned.
If this method is called twice on the same object, and the object is
not modified in between times, the iterators produced may or may not be
the same iterator, and may or may not return the elements in the same
order, but must return the same objects.
- Specified by:
iterator
in interface Iterable
run
public void run()
- Specified by:
run
in interface Runnable
doWork
protected abstract void doWork()
- The implementation should call
QueueIterator.put(java.lang.Object)
with each row.
start
protected ThreadIterator start()
onEndOfQueue
protected void onEndOfQueue()
- Description copied from class:
QueueIterator
- Called (from the consumer thread context) just before the iterator
returns false for hasNext(). Default implementation does nothing, but
subclasses can use this for cleanup actions.
- Overrides:
onEndOfQueue
in class QueueIterator