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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
WorkQueue.WorkUnitProcessor<T>
A work unit processor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addWorkUnit(T workUnit)
Add a unit of work.void
addWorkUnits(Collection<T> workUnits)
Add multiple units of work.void
close()
Ensure that there are no work units still uncompleted.static <U> void
runWorkQueue(Collection<U> elements, ExecutorService executorService, int numParallelTasks, WorkQueue.WorkUnitProcessor<U> workUnitProcessor, InterruptionChecker interruptionChecker, LogNode log)
Start a work queue on the elements in the provided collection, blocking until all work units have been completed.
-
-
-
Method Detail
-
runWorkQueue
public static <U> void runWorkQueue(Collection<U> elements, ExecutorService executorService, int numParallelTasks, WorkQueue.WorkUnitProcessor<U> workUnitProcessor, InterruptionChecker interruptionChecker, LogNode log) throws ExecutionException, InterruptedException
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
.numParallelTasks
- The number of parallel tasks.workUnitProcessor
- TheWorkQueue.WorkUnitProcessor
.interruptionChecker
- TheInterruptionChecker
.log
- The log.- Throws:
ExecutionException
- If an exception is thrown while processing a work unit.InterruptedException
- If the work was interrupted.
-
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.
-
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.
-
close
public void close()
Ensure that there are no work units still uncompleted. This should be called after runWorkLoop() exits on the main thread (e.g. using try-with-resources, since this class is AutoCloseable). If any work units are still uncompleted (e.g. in the case of an exception), will shut down remaining workers.- Specified by:
close
in interfaceAutoCloseable
-
-