saffron.runtime
Class ThreadIterator

java.lang.Object
  |
  +--saffron.runtime.QueueIterator
        |
        +--saffron.runtime.ThreadIterator
All Implemented Interfaces:
Iterable, Iterator, Runnable
Direct Known Subclasses:
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
static class ThreadIterator.Test
          Test harness for ThreadIterator.
 
Constructor Summary
ThreadIterator()
           
 
Method Summary
 void done(Throwable throwable)
          Producer calls done to say that there are no more objects, setting throwable if there was an error.
protected abstract  void doWork()
          The implementation should call QueueIterator.put(java.lang.Object) with each row.
 boolean hasNext()
           
 Iterator iterator()
          Returns an iterator over the elements in this collection.
 Object next()
           
 void put(Object o)
          Producer calls put to add another object (which may be null).
 void remove()
           
 void run()
           
protected  ThreadIterator start()
           
 
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
 

Constructor Detail

ThreadIterator

public ThreadIterator()
Method Detail

start

protected ThreadIterator start()

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

doWork

protected abstract void doWork()
The implementation should call QueueIterator.put(java.lang.Object) with each row.


run

public void run()
Specified by:
run in interface Runnable

hasNext

public boolean hasNext()
Specified by:
hasNext in interface Iterator

next

public Object next()
Specified by:
next in interface Iterator

remove

public void remove()
Specified by:
remove in interface Iterator

put

public void put(Object o)
Producer calls put to add another object (which may be null).


done

public void done(Throwable throwable)
Producer calls done to say that there are no more objects, setting throwable if there was an error.


SourceForge.net_Logo