I - the type of the input to be processed.F - the type of the factory for the input processorspublic class ConcurrentComputation<I,F extends InputProcessorFactory<I,?>> extends SimpleInterrupter
InputProcessor objects created by the supplied
InputProcessorFactory. The implementation is loosely based on a
producer-consumer framework with one producer and many consumers. The
processing of the input should start by calling the start() method,
following by submit(Object) method for submitting input to be
processed. The workers will always wait for new input, unless interrupted or
finish() method is called. If finish() is called then no
further input can be submitted and the workers will terminate when all input
has been processed or they are interrupted earlier, whichever is earlier.| Constructor and Description |
|---|
ConcurrentComputation(F inputProcessorFactory,
ComputationExecutor executor,
int maxWorkers)
Creating a
ConcurrentComputation instance. |
ConcurrentComputation(F inputProcessorFactory,
ComputationExecutor executor,
int maxWorkers,
int bufferCapacity)
Creating a
ConcurrentComputation instance. |
| Modifier and Type | Method and Description |
|---|---|
void |
finish()
Marks the end of the input and requests all workers to terminate when all
currently submitted input has been processed.
|
F |
getInputProcessorFactory() |
void |
setInterrupt(boolean flag)
Set interrupt flag to the given value.
|
boolean |
start()
Starts the workers to process the input.
|
boolean |
submit(I input)
Submitting a new input for processing.
|
protected void |
waitWorkers()
Wait until all currently running workers stop; no input can be submitted
after calling this method.
|
isInterruptedpublic ConcurrentComputation(F inputProcessorFactory, ComputationExecutor executor, int maxWorkers, int bufferCapacity)
ConcurrentComputation instance.inputProcessorFactory - the factory for input processorsexecutor - the executor used internally to run the jobsmaxWorkers - the maximal number of concurrent workers processing the jobsbufferCapacity - the size of the buffer for scheduled jobs; if the buffer is
full, submitting new jobs will block until new space is
availablepublic ConcurrentComputation(F inputProcessorFactory, ComputationExecutor executor, int maxWorkers)
ConcurrentComputation instance.inputProcessorFactory - the factory for input processorsexecutor - the executor used internally to run the jobsmaxWorkers - the maximal number of concurrent workers processing the jobspublic F getInputProcessorFactory()
InputProcessorFactory used by this
ConcurrentComputationpublic boolean start()
true if the operation was successfulpublic boolean submit(I input) throws InterruptedException
input - the input to be processedtrue if the input has been successfully submitted for
computation; the input cannot be submitted if finish()
was called or the computation has been terminatedInterruptedException - thrown if interrupted during waiting for space to be
availablepublic final void setInterrupt(boolean flag)
InterrupterInterrupter.isInterrupted() should return this valuesetInterrupt in interface InterruptersetInterrupt in class SimpleInterrupterflag - true if interrupt is requested false if notprotected void waitWorkers()
throws InterruptedException
InterruptedExceptionpublic void finish()
throws InterruptedException
submit(Object) will always return false. The method
blocks until all workers have been stopped. If interrupted while blocked,
this method can be called again in order to complete the termination
request.InterruptedException - if interrupted during waiting for finish requestCopyright © 2011-2015 Department of Computer Science, University of Oxford. All Rights Reserved.