Class WorkQueue<T>

java.lang.Object
nonapi.io.github.classgraph.concurrency.WorkQueue<T>
Type Parameters:
T - The work unit type.
All Implemented Interfaces:
AutoCloseable

public class WorkQueue<T> extends Object implements AutoCloseable
A parallel work queue.
  • Field Details

    • workUnitProcessor

      private final WorkQueue.WorkUnitProcessor<T> workUnitProcessor
      The work unit processor.
    • workUnits

      private final BlockingQueue<WorkQueue.WorkUnitWrapper<T>> workUnits
      The queue of work units.
    • numWorkers

      private final int numWorkers
      The number of workers.
    • numIncompleteWorkUnits

      private final AtomicInteger numIncompleteWorkUnits
      The number of work units remaining to be processed, plus the number of currently running threads working on a work unit.
    • workerFutures

      private final ConcurrentLinkedQueue<Future<?>> workerFutures
      The Future object added for each worker, used to detect worker completion.
    • interruptionChecker

      private final InterruptionChecker interruptionChecker
      The shared InterruptionChecker, used to detect thread interruption and execution exceptions, and to shut down all threads if either of these occurs.
    • log

      private final LogNode log
      The log node.
  • Constructor Details

    • WorkQueue

      private WorkQueue(Collection<T> initialWorkUnits, WorkQueue.WorkUnitProcessor<T> workUnitProcessor, int numWorkers, InterruptionChecker interruptionChecker, LogNode log)
      A parallel work queue.
      Parameters:
      initialWorkUnits - the initial work units
      workUnitProcessor - the work unit processor
      numWorkers - the number of workers
      interruptionChecker - the interruption checker
      log - the log
  • Method Details

    • runWorkQueue

      public static <U> void runWorkQueue(Collection<U> elements, ExecutorService executorService, InterruptionChecker interruptionChecker, int numParallelTasks, LogNode log, WorkQueue.WorkUnitProcessor<U> workUnitProcessor) throws InterruptedException, ExecutionException
      Start a work queue on the elements in the provided collection, blocking until all work units have been completed.
      Type Parameters:
      U - The type of the work queue units.
      Parameters:
      elements - The work queue units to process.
      executorService - The ExecutorService.
      interruptionChecker - the interruption checker
      numParallelTasks - The number of parallel tasks.
      log - The log.
      workUnitProcessor - The WorkQueue.WorkUnitProcessor.
      Throws:
      InterruptedException - If the work was interrupted.
      ExecutionException - If a worker throws an uncaught exception.
    • startWorkers

      private void startWorkers(ExecutorService executorService, int numTasks)
      Start worker threads with a shared log.
      Parameters:
      executorService - the executor service
      numTasks - the number of worker tasks to start
    • sendPoisonPills

      private void sendPoisonPills()
      Send poison pills to workers.
    • runWorkLoop

      private void runWorkLoop() throws InterruptedException, ExecutionException
      Start a worker. Called by startWorkers(), but should also be called by the main thread to do some of the work on that thread, to prevent deadlock in the case that the ExecutorService doesn't have as many threads available as numParallelTasks. When this method returns, either all the work has been completed, or this or some other thread was interrupted. If InterruptedException is thrown, this thread or another was interrupted.
      Throws:
      InterruptedException - if a worker thread was interrupted
      ExecutionException - if a worker thread throws an uncaught exception
    • addWorkUnit

      public void addWorkUnit(T workUnit)
      Add a unit of work. May be called by workers to add more work units to the tail of the queue.
      Parameters:
      workUnit - the work unit
      Throws:
      NullPointerException - if the work unit is null.
    • addWorkUnits

      public void addWorkUnits(Collection<T> workUnits)
      Add multiple units of work. May be called by workers to add more work units to the tail of the queue.
      Parameters:
      workUnits - The work units to add to the tail of the queue.
      Throws:
      NullPointerException - if any of the work units are null.
    • close

      public void close() throws ExecutionException
      Completion barrier for work queue. This should be called after runWorkLoop() exits on the main thread (e.g. using try-with-resources).
      Specified by:
      close in interface AutoCloseable
      Throws:
      ExecutionException - If a worker threw an uncaught exception.