Class ScheduledTasksIterator<T>

java.lang.Object
org.variantsync.diffdetective.parallel.ScheduledTasksIterator<T>
All Implemented Interfaces:
AutoCloseable, Iterator<T>

public class ScheduledTasksIterator<T> extends Object implements Iterator<T>, AutoCloseable
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 Details

  • Constructor Details

    • ScheduledTasksIterator

      public ScheduledTasksIterator(Iterator<? extends Callable<T>> tasks, int nThreads)
      Starts scheduling tasks in nThreads other threads. The results of these tasks can be retrieved by calling next(). The order of these results is deterministic and is the same as the order of the provided tasks.
      Parameters:
      tasks - the tasks which will be executed in other threads
      nThreads - the number of threads which work on tasks in parallel
    • ScheduledTasksIterator

      public ScheduledTasksIterator(Iterable<? extends Callable<T>> tasks, int nThreads)
      Starts scheduling tasks in nThreads other threads.
      Parameters:
      tasks - the tasks which will be executed in other threads
      nThreads - the number of threads which work on tasks in parallel
  • Method Details

    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<T>
    • next

      public T next()
      Waits for the next task and retrieves its result. The order of the results corresponds to the order of tasks given in the constructor. Each call to next schedules a new task if any task is remaining.
      Specified by:
      next in interface Iterator<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:
      close in interface AutoCloseable