Class WorkQueue<T>
- java.lang.Object
-
- io.github.lukehutch.fastclasspathscanner.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.
-
Constructor Summary
Constructors Constructor Description WorkQueue(Collection<T> initialWorkUnits, WorkQueue.WorkUnitProcessor<T> workUnitProcessor, InterruptionChecker interruptionChecker, LogNode log)
A parallel work queue.
-
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.void
runWorkLoop()
Start a worker.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.void
startWorkers(ExecutorService executorService, int numWorkers, LogNode log)
Start worker threads with a shared log.
-
-
-
Constructor Detail
-
WorkQueue
public WorkQueue(Collection<T> initialWorkUnits, WorkQueue.WorkUnitProcessor<T> workUnitProcessor, InterruptionChecker interruptionChecker, LogNode log)
A parallel work queue.
-
-
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.
-
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.
-
startWorkers
public void startWorkers(ExecutorService executorService, int numWorkers, LogNode log)
Start worker threads with a shared log.
-
runWorkLoop
public 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.
-
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.
-
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
-
-