Class WorkQueue<T>
java.lang.Object
nonapi.io.github.classgraph.concurrency.WorkQueue<T>
- Type Parameters:
T
- The work unit type.
- All Implemented Interfaces:
AutoCloseable
A parallel work queue.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
A work unit processor.private static class
A wrapper for work units (needed to send a poison pill as a null value, since BlockingQueue does not accept null values). -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final InterruptionChecker
The shared InterruptionChecker, used to detect thread interruption and execution exceptions, and to shut down all threads if either of these occurs.private final LogNode
The log node.private final AtomicInteger
The number of work units remaining to be processed, plus the number of currently running threads working on a work unit.private final int
The number of workers.private final ConcurrentLinkedQueue
<Future<?>> The Future object added for each worker, used to detect worker completion.private final WorkQueue.WorkUnitProcessor
<T> The work unit processor.private final BlockingQueue
<WorkQueue.WorkUnitWrapper<T>> The queue of work units. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
WorkQueue
(Collection<T> initialWorkUnits, WorkQueue.WorkUnitProcessor<T> workUnitProcessor, int numWorkers, InterruptionChecker interruptionChecker, LogNode log) A parallel work queue. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addWorkUnit
(T workUnit) Add a unit of work.void
addWorkUnits
(Collection<T> workUnits) Add multiple units of work.void
close()
Completion barrier for work queue.private void
Start a worker.static <U> void
runWorkQueue
(Collection<U> elements, ExecutorService executorService, InterruptionChecker interruptionChecker, int numParallelTasks, LogNode log, WorkQueue.WorkUnitProcessor<U> workUnitProcessor) Start a work queue on the elements in the provided collection, blocking until all work units have been completed.private void
Send poison pills to workers.private void
startWorkers
(ExecutorService executorService, int numTasks) Start worker threads with a shared log.
-
Field Details
-
workUnitProcessor
The work unit processor. -
workUnits
The queue of work units. -
numWorkers
private final int numWorkersThe number of workers. -
numIncompleteWorkUnits
The number of work units remaining to be processed, plus the number of currently running threads working on a work unit. -
workerFutures
The Future object added for each worker, used to detect worker completion. -
interruptionChecker
The shared InterruptionChecker, used to detect thread interruption and execution exceptions, and to shut down all threads if either of these occurs. -
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 unitsworkUnitProcessor
- the work unit processornumWorkers
- the number of workersinterruptionChecker
- the interruption checkerlog
- 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
- TheExecutorService
.interruptionChecker
- the interruption checkernumParallelTasks
- The number of parallel tasks.log
- The log.workUnitProcessor
- TheWorkQueue.WorkUnitProcessor
.- Throws:
InterruptedException
- If the work was interrupted.ExecutionException
- If a worker throws an uncaught exception.
-
startWorkers
Start worker threads with a shared log.- Parameters:
executorService
- the executor servicenumTasks
- the number of worker tasks to start
-
sendPoisonPills
private void sendPoisonPills()Send poison pills to workers. -
runWorkLoop
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 interruptedExecutionException
- if a worker thread throws an uncaught exception
-
addWorkUnit
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
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
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 interfaceAutoCloseable
- Throws:
ExecutionException
- If a worker threw an uncaught exception.
-