Class ScheduledTasksIterator<T>
java.lang.Object
org.variantsync.diffdetective.parallel.ScheduledTasksIterator<T>
- All Implemented Interfaces:
AutoCloseable,Iterator<T>
An iterator over the results of computations run in other threads.
The results of the given tasks can be received in the correct order using
next(). That
means the next() method is deterministic if all tasks are deterministic.
Upon creation of a ScheduledTasksIterator, all given tasks are scheduled for execution. However, the results of these tasks become available in the same order as the tasks have been provided. This means, that the iterator will block until the result of the next task is available, even if all other remaining tasks are complete.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final LinkedList<Future<T>> private final ExecutorService -
Constructor Summary
ConstructorsConstructorDescriptionScheduledTasksIterator(Iterable<? extends Callable<T>> tasks, int nThreads) Starts schedulingtasksinnThreadsother threads.ScheduledTasksIterator(Iterator<? extends Callable<T>> tasks, int nThreads) Starts schedulingtasksinnThreadsother threads. -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Iterator
forEachRemaining, remove
-
Field Details
-
remainingTasks
-
futures
-
threadPool
-
-
Constructor Details
-
ScheduledTasksIterator
Starts schedulingtasksinnThreadsother threads. The results of these tasks can be retrieved by callingnext(). The order of these results is deterministic and is the same as the order of the providedtasks.- Parameters:
tasks- the tasks which will be executed in other threadsnThreads- the number of threads which work ontasksin parallel
-
ScheduledTasksIterator
Starts schedulingtasksinnThreadsother threads.- Parameters:
tasks- the tasks which will be executed in other threadsnThreads- the number of threads which work ontasksin parallel
-
-
Method Details
-
hasNext
public boolean hasNext() -
next
Waits for the next task and retrieves its result. The order of the results corresponds to the order of tasks given in theconstructor. Each call tonextschedules a new task if any task is remaining.- Specified by:
nextin interfaceIterator<T>- Returns:
- one result of a tasks given in
ScheduledTasksIterator - Throws:
RuntimeException- if a thread is interrupted or a task couldn't be executed
-
close
public void close()Stops all scheduled tasks and releases the used thread resources.- Specified by:
closein interfaceAutoCloseable
-