Package io.github.classgraph.utils
Class WorkQueue<T>
- java.lang.Object
-
- io.github.classgraph.utils.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.WorkQueuePreStartHook<T>
A hook that is called once a WorkQueue is created, inside its try-with-resources block, before the workers are started.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
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.static <U> void
runWorkQueue(Collection<U> elements, ExecutorService executorService, int numParallelTasks, WorkQueue.WorkUnitProcessor<U> workUnitProcessor, WorkQueue.WorkQueuePreStartHook<U> workQueuePreStartHook, 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, WorkQueue.WorkQueuePreStartHook<U> workQueuePreStartHook, 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. Calls workQueuePreStartHook with a ref to the work queue before starting the workers.- 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
.workQueuePreStartHook
- TheWorkQueue.WorkQueuePreStartHook
.interruptionChecker
- TheInterruptionChecker
.log
- The log.- Throws:
ExecutionException
- If an exception is thrown while processing a work unit.InterruptedException
- If the work was interrupted.
-
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.
-
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() throws ExecutionException
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
- Throws:
ExecutionException
-
-