org.eigenbase.runtime
Class TimeoutQueueIterator

java.lang.Object
  extended by org.eigenbase.runtime.TimeoutQueueIterator
Direct Known Subclasses:
QueueIteratorTest.TestingTimeoutQueueIterator

public class TimeoutQueueIterator
extends Object

Adapter which allows you to iterate over an Iterator with a timeout.

The interface is similar to an Iterator: the hasNext(long) method tests whether there are more rows, and the next(long) method gets the next row. Each has a timeout parameter, and throws a QueueIterator.TimeoutException if the timeout is exceeded. There is also a close(long) method, which you must call.

The class is implemented using a thread which reads from the underlying iterator and places the results into a QueueIterator. If a method call times out, the underlying thread will wait for the result of the call until it completes.

There is no facility to cancel the fetch from the underlying iterator.

Since:
Jun 20, 2004
Version:
$Id: //open/dev/farrago/src/org/eigenbase/runtime/TimeoutQueueIterator.java#13 $
Author:
tleung
"JUnit testcase:"

Field Summary
private  Iterator producer
           
protected  QueueIterator queueIterator
           
private  Thread thread
           
 
Constructor Summary
TimeoutQueueIterator(Iterator producer)
           
 
Method Summary
 void close(long timeoutMillis)
          Releases the resources used by this iterator, including killing the underlying thread.
private  void doWork()
          Reads objects from the producer and writes them into the QueueIterator.
 boolean hasNext(long timeoutMillis)
          Returns whether the producer has another row, if that can be determined within the timeout interval.
 Object next(long timeoutMillis)
          Returns the next row from the producer, if it can be fetched within the timeout interval.
 void start()
          Starts the thread which reads from the consumer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

queueIterator

protected final QueueIterator queueIterator

producer

private final Iterator producer

thread

private Thread thread
Constructor Detail

TimeoutQueueIterator

public TimeoutQueueIterator(Iterator producer)
Method Detail

hasNext

public boolean hasNext(long timeoutMillis)
                throws QueueIterator.TimeoutException
Returns whether the producer has another row, if that can be determined within the timeout interval.

Parameters:
timeoutMillis - Millisonds to wait; less than or equal to zero means don't wait
Throws:
QueueIterator.TimeoutException - if producer does not answer within the timeout interval

next

public Object next(long timeoutMillis)
            throws QueueIterator.TimeoutException
Returns the next row from the producer, if it can be fetched within the timeout interval.

Throws:
QueueIterator.TimeoutException - if producer does not answer within the timeout interval

start

public void start()
Starts the thread which reads from the consumer.

"Precondition:"
thread == null // not previously started

close

public void close(long timeoutMillis)
Releases the resources used by this iterator, including killing the underlying thread.

Parameters:
timeoutMillis - Timeout while waiting for the underlying thread to die. Zero means wait forever.

doWork

private void doWork()
Reads objects from the producer and writes them into the QueueIterator. This is the method called by the thread when you call start(). Never throws an exception.