org.eigenbase.runtime
Class ThreadIterator

java.lang.Object
  extended by org.eigenbase.runtime.QueueIterator
      extended by 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.
 }


Nested Class Summary
 
Nested classes/interfaces inherited from class org.eigenbase.runtime.QueueIterator
QueueIterator.TimeoutException
 
Field Summary
private  Thread thread
           
 
Fields inherited from class org.eigenbase.runtime.QueueIterator
hasNext, next, queue, throwable
 
Constructor Summary
ThreadIterator()
           
ThreadIterator(BlockingQueue queue)
           
 
Method Summary
protected abstract  void doWork()
          The implementation should call QueueIterator.put(java.lang.Object) with each row.
 Iterator iterator()
          Returns an iterator over the elements in this collection.
protected  void onEndOfQueue()
          Called (from the consumer thread context) just before the iterator returns false for hasNext().
 void run()
           
protected  ThreadIterator start()
           
 
Methods inherited from class org.eigenbase.runtime.QueueIterator
checkTermination, done, hasNext, hasNext, next, next, offer, put, remove, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Iterator
hasNext, next, remove
 

Field Detail

thread

private Thread thread
Constructor Detail

ThreadIterator

public ThreadIterator()

ThreadIterator

public ThreadIterator(BlockingQueue queue)
Method Detail

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