Top Description Inners Fields Constructors Methods
nonapi.io.github.classgraph.concurrency

public Class WorkQueue<T>

extends Object
implements AutoCloseable
Class Inheritance
All Implemented Interfaces
java.lang.AutoCloseable
Type Parameters
<T>
The work unit type.
Imports
java.util.Collection, java.util.concurrent.BlockingQueue, .Callable, .CancellationException, .ConcurrentLinkedQueue, .ExecutionException, .ExecutorService, .Future, .LinkedBlockingQueue, java.util.concurrent.atomic.AtomicInteger, nonapi.io.github.classgraph.utils.LogNode

A parallel work queue.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
WorkQueue.WorkUnitProcessor<
The type of work unit to process.
T
>

A work unit processor.

private static class
WorkQueue.WorkUnitWrapper<
the generic type
T
>

A wrapper for work units (needed to send a poison pill as a null value, since BlockingQueue does not accept null values).

Field Summary

Modifier and TypeField and Description
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.

private final LogNode
log

The log node.

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.

private final int
numWorkers

The number of workers.

private final ConcurrentLinkedQueue<Future<?>>
workerFutures

The Future object added for each worker, used to detect worker completion.

private final WorkQueue.WorkUnitProcessor<T>
workUnitProcessor

The work unit processor.

private final BlockingQueue<WorkQueue.WorkUnitWrapper<T>>
workUnits

The queue of work units.

Constructor Summary

AccessConstructor and Description
private
WorkQueue(final Collection<T>
the initial work units
initialWorkUnits
,
final WorkQueue.WorkUnitProcessor<T>
the work unit processor
workUnitProcessor
,
final int
the number of workers
numWorkers
,
final InterruptionChecker
the interruption checker
interruptionChecker
,
final LogNode
the log
log
)

A parallel work queue.

Method Summary

Modifier and TypeMethod and Description
public void
addWorkUnit(final T
the work unit
workUnit
)

Add a unit of work.

public void
addWorkUnits(final Collection<T>
The work units to add to the tail of the queue.
workUnits
)

Add multiple units of work.

public void
close()

Implements java.lang.AutoCloseable.close.

Completion barrier for work queue.

private void
runWorkLoop()

Start a worker.

public static <
The type of the work queue units.
U
>
void
runWorkQueue(final Collection<U>
The work queue units to process.
elements
,
final ExecutorService executorService, final InterruptionChecker
the interruption checker
interruptionChecker
,
final int
The number of parallel tasks.
numParallelTasks
,
final LogNode
The log.
log
,
final 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
sendPoisonPills()

Send poison pills to workers.

private void
startWorkers(final ExecutorService
the executor service
executorService
,
final int
the number of worker tasks to start
numTasks
)

Start worker threads with a shared log.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

interruptionCheckerback to summary
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.

logback to summary
private final LogNode log

The log node.

numIncompleteWorkUnitsback to summary
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.

numWorkersback to summary
private final int numWorkers

The number of workers.

workerFuturesback to summary
private final ConcurrentLinkedQueue<Future<?>> workerFutures

The Future object added for each worker, used to detect worker completion.

workUnitProcessorback to summary
private final WorkQueue.WorkUnitProcessor<T> workUnitProcessor

The work unit processor.

workUnitsback to summary
private final BlockingQueue<WorkQueue.WorkUnitWrapper<T>> workUnits

The queue of work units.

Constructor Detail

WorkQueueback to summary
private WorkQueue(final Collection<T> initialWorkUnits, final WorkQueue.WorkUnitProcessor<T> workUnitProcessor, final int numWorkers, final InterruptionChecker interruptionChecker, final LogNode log)

A parallel work queue.

Parameters
initialWorkUnits:Collection<T>

the initial work units

workUnitProcessor:WorkQueue.WorkUnitProcessor<T>

the work unit processor

numWorkers:int

the number of workers

interruptionChecker:InterruptionChecker

the interruption checker

log:LogNode

the log

Method Detail

addWorkUnitback to summary
public void addWorkUnit(final 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:T

the work unit

Exceptions
NullPointerException:
if the work unit is null.
addWorkUnitsback to summary
public void addWorkUnits(final 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:Collection<T>

The work units to add to the tail of the queue.

Exceptions
NullPointerException:
if any of the work units are null.
closeback to summary
public void close() throws ExecutionException

Implements java.lang.AutoCloseable.close.

Completion barrier for work queue. This should be called after runWorkLoop() exits on the main thread (e.g. using try-with-resources).

Annotations
@Override
Exceptions
ExecutionException:
If a worker threw an uncaught exception.
runWorkLoopback to summary
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.

Exceptions
InterruptedException:
if a worker thread was interrupted
ExecutionException:
if a worker thread throws an uncaught exception
runWorkQueueback to summary
public static <U> void runWorkQueue(final Collection<U> elements, final ExecutorService executorService, final InterruptionChecker interruptionChecker, final int numParallelTasks, final LogNode log, final 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.

Parameters
<U>
The type of the work queue units.
elements:Collection<U>

The work queue units to process.

executorService:ExecutorService

The ExecutorService.

interruptionChecker:InterruptionChecker

the interruption checker

numParallelTasks:int

The number of parallel tasks.

log:LogNode

The log.

workUnitProcessor:WorkQueue.WorkUnitProcessor<U>

The WorkUnitProcessor.

Exceptions
InterruptedException:
If the work was interrupted.
ExecutionException:
If a worker throws an uncaught exception.
sendPoisonPillsback to summary
private void sendPoisonPills()

Send poison pills to workers.

Annotations
@SuppressWarnings:null
startWorkersback to summary
private void startWorkers(final ExecutorService executorService, final int numTasks)

Start worker threads with a shared log.

Parameters
executorService:ExecutorService

the executor service

numTasks:int

the number of worker tasks to start

nonapi.io.github.classgraph.concurrency back to summary

public Interface WorkQueue.WorkUnitProcessor<T>

Known Direct Implementers
io.github.classgraph.Scanner.ClassfileScannerWorkUnitProcessor
Type Parameters
<T>
The type of work unit to process.

A work unit processor.

Method Summary

Modifier and TypeMethod and Description
public void
processWorkUnit(T
The work unit.
workUnit
,
WorkQueue<T>
The work queue.
workQueue
,
LogNode
The log.
log
)

Process a work unit.

Method Detail

processWorkUnitback to summary
public void processWorkUnit(T workUnit, WorkQueue<T> workQueue, LogNode log) throws InterruptedException

Process a work unit.

Parameters
workUnit:T

The work unit.

workQueue:WorkQueue<T>

The work queue.

log:LogNode

The log.

Exceptions
InterruptedException:
If the worker thread is interrupted.
nonapi.io.github.classgraph.concurrency back to summary

private Class WorkQueue.WorkUnitWrapper<T>

extends Object
Class Inheritance
  • java.lang.Object
  • nonapi.io.github.classgraph.concurrency.WorkQueue.WorkUnitWrapper
Type Parameters
<T>
the generic type

A wrapper for work units (needed to send a poison pill as a null value, since BlockingQueue does not accept null values).

Field Summary

Modifier and TypeField and Description
pack-priv final T
workUnit

The work unit.

Constructor Summary

AccessConstructor and Description
public
WorkUnitWrapper(final T
the work unit, or null to represent a poison pill.
workUnit
)

Constructor.

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

workUnitback to summary
pack-priv final T workUnit

The work unit.

Constructor Detail

WorkUnitWrapperback to summary
public WorkUnitWrapper(final T workUnit)

Constructor.

Parameters
workUnit:T

the work unit, or null to represent a poison pill.