Class AbstractObjectRunnable<O1,O2,R,E extends AbstractObjectSpecificExecutor<O1,O2,R>>
java.lang.Object
de.aristaflow.adept2.util.threading.executor.AbstractObjectRunnable<O1,O2,R,E>
- Type Parameters:
O1- The type of objects for which runnables are registered.O2- The type of objects with which runnables are managed and executed within an object-specific executor service.R- The runtime data for managed objects. This allows for arbitrary data for each managed object.E- The executor managing this runnable and eventually running it.
- All Implemented Interfaces:
ManagedRunnable,ObjectRunnable<O1,,O2> Runnable
- Direct Known Subclasses:
ObjectOneWorkRunnable,PipelineRunnable,ScheduledObjectOneWorkRunnable
public abstract class AbstractObjectRunnable<O1,O2,R,E extends AbstractObjectSpecificExecutor<O1,O2,R>>
extends Object
implements ObjectRunnable<O1,O2>
An abstract implementation for an
ObjectRunnable including some lifecycle management
like counting the executions and signalling the termination.-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final EThe executor to which to (re-)add this runnable if required (more work present, delay has elapsed, ...).protected final LoggerThe logger for various purpose, e.The data structures containing all objects and the corresponding work.protected final AtomicIntegerThe amount of termination requests, that is, the amount of pending flushs which abort the work of all objects. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractObjectRunnable(O1 registeredObject, O2 indexObject, E executor, AtomicInteger terminate, Logger logger) Creates a new runnable for the designated object.protectedAbstractObjectRunnable(O1 registeredObject, O2 indexObject, Map<O2, Pair<ObjectRunnable<O1, O2>, R>> objects, AtomicInteger terminate, Logger logger) Deprecated, for removal: This API element is subject to removal in a future version. -
Method Summary
Modifier and TypeMethodDescriptionvoidAwaits the termination of this runnable.protected voidProcesses the rejection of the last execution of this runnable, that is, the bookkeeping which would have taken place after this execution.protected intGets the amount of executions of this runnable which have been started by the current thread.Gets the object with which this runnable is managed and executed within an object-specific executor service.Gets the object with which this runnable has been registered at an object-specific executor service.voidrejected()Signals that submitting an execution of this runnable has failed, that is, the pool has rejected it.voidrun()protected abstract booleanRuns this runnable actually.voidSignals that an execution of this runnable has been submitted, that is, handed to a pool for execution.
-
Field Details
-
executor
The executor to which to (re-)add this runnable if required (more work present, delay has elapsed, ...). -
objects
The data structures containing all objects and the corresponding work. -
terminate
The amount of termination requests, that is, the amount of pending flushs which abort the work of all objects. If this is > 0, the runnable has to terminate, that is, it must neither start nor continue to execute work.
A terminating runnable must not change the work map either. The aborting flush removes the entries without work pending for after the flush and re-submits the runnables of the remaining entries when restarting, so it owns the work map as long as it is pending.
Access has to be synchronised viaobjects. -
logger
The logger for various purpose, e. g. logging exceptions which are not propagated to outside the runnable.
-
-
Constructor Details
-
AbstractObjectRunnable
@Deprecated(since="18.0.0", forRemoval=true) protected AbstractObjectRunnable(O1 registeredObject, O2 indexObject, Map<O2, Pair<ObjectRunnable<O1, O2>, R>> objects, AtomicInteger terminate, Logger logger) Deprecated, for removal: This API element is subject to removal in a future version.UseAbstractObjectRunnable(Object, Object, AbstractObjectSpecificExecutor, AtomicInteger, Logger)instead. A runnable created without an executor cannot re-submit itself when required.Creates a new runnable for the designated object.- Parameters:
registeredObject- The object for which this runnable has been registered.indexObject- The object with which this runnable is managed and executed within the corresponding object-specific executor service.objects- The data structures containing all objects and the corresponding work.terminate- The amount of termination requests. If this is > 0, the runnable has to terminate without changing the work map.logger- The logger for various purpose, e. g. logging exceptions which are not propagated to outside the runnable.
-
AbstractObjectRunnable
protected AbstractObjectRunnable(O1 registeredObject, O2 indexObject, E executor, AtomicInteger terminate, Logger logger) Creates a new runnable for the designated object.- Parameters:
registeredObject- The object for which this runnable has been registered.indexObject- The object with which this runnable is managed and executed within the corresponding object-specific executor service.executor- The executor to which to (re-)add this runnable if required.terminate- The amount of termination requests. If this is > 0, the runnable has to terminate without changing the work map.logger- The logger for various purpose, e. g. logging exceptions which are not propagated to outside the runnable.
-
-
Method Details
-
getRegisteredObject
Description copied from interface:ObjectRunnableGets the object with which this runnable has been registered at an object-specific executor service.- Specified by:
getRegisteredObjectin interfaceObjectRunnable<O1,O2> - Returns:
- The object with which this runnable has been registered at an object-specific executor service.
-
getIndexObject
Description copied from interface:ObjectRunnableGets the object with which this runnable is managed and executed within an object-specific executor service.- Specified by:
getIndexObjectin interfaceObjectRunnable<O1,O2> - Returns:
- The object with which this runnable is managed and executed within an object-specific executor service.
-
run
public void run() -
submitted
public void submitted()Description copied from interface:ManagedRunnableSignals that an execution of this runnable has been submitted, that is, handed to a pool for execution. The execution counts for the termination from now on, that is, for the time between being handed to the pool and being started (or rejected) by it. Awaiting the termination therefore awaits the execution although it has not started yet: work which has been submitted before a flush completes before the task of the flush and no work runs concurrently to this task.- Specified by:
submittedin interfaceManagedRunnable
-
rejected
public void rejected()Description copied from interface:ManagedRunnableSignals that submitting an execution of this runnable has failed, that is, the pool has rejected it. This revertsManagedRunnable.submitted()since the execution will not take place.
Nothing is reverted if the execution has started meanwhile, e. g. a handler for rejected executions which executes the task within the calling thread:Runnable.run()counts such an execution itself, so reverting would count it twice.- Specified by:
rejectedin interfaceManagedRunnable
-
executionRejected
protected void executionRejected()Processes the rejection of the last execution of this runnable, that is, the bookkeeping which would have taken place after this execution. The runnable will not be executed, so its object or pipeline would keep it without it being submitted and no work would ever be executed for this object or pipeline again.This is only called when no execution of this runnable is submitted or in progress any more, so no other execution takes care of the work. An execution which is rejected while another one is in progress does not get here, the execution in progress does the bookkeeping itself.
Implementations have to acquire the lock onobjectsthemselves; the lock on the termination is not held. The default implementation does nothing. -
getExecutionsStartedByCurrentThread
protected int getExecutionsStartedByCurrentThread()Gets the amount of executions of this runnable which have been started by the current thread. The count only ever increases and is confined to the calling thread: between two reads of one thread it changes only if this thread has started an execution in between, e. g. via a handler for rejected executions which executes a rejected task within the calling thread.- Returns:
- The amount of executions of this runnable which have been started by the current thread.
-
runForObject
protected abstract boolean runForObject()Runs this runnable actually. Counting the executions and signalling the termination are already handled, implementations only need to react to termination requests.An execution which has been submitted but has not started yet is awaited by a flush as well: it executes the work of its object as soon as it starts, so work which has been submitted before the flush completes before the task of the flush and no work runs concurrently to this task.
- Returns:
- Whether the work for the object has been terminated. Otherwise there is work left and
another execution of this runnable continues it. With a termination request, return
true, only returnfalseif you want to continue to work without interruption, e. g. to change the executing thread. The return value is informational, the termination is signalled based on the submitted and running executions.
-
awaitTermination
Description copied from interface:ObjectRunnableAwaits the termination of this runnable. Either the thread has been interrupted, its termination has been requested or the runnable has simply finished. If this runnable is neither being executed nor submitted for execution, this method returns immediately. An execution which has been submitted, that is, handed to a pool, but has not started yet is awaited as well: it executes the work of its object as soon as it starts, so a flush awaiting the termination runs its task only after the pending work and no work runs concurrently to this task.
A runnable may be executed again, e. g. when restarting after a flush. This method awaits the termination of the current and the submitted executions then, a previous termination does not count.- Specified by:
awaitTerminationin interfaceObjectRunnable<O1,O2> - Throws:
InterruptedException- If the waiting thread is interrupted while waiting for this runnable to terminate, anInterruptedExceptionwill be thrown.
-
AbstractObjectRunnable(Object, Object, AbstractObjectSpecificExecutor, AtomicInteger, Logger)instead.