net.sf.saffron.runtime
Class ThreadIterator
java.lang.Object
|
+--net.sf.saffron.runtime.QueueIterator
|
+--net.sf.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.
}
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
ThreadIterator
public ThreadIterator()
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()