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 Details

    • executor

      protected final E extends AbstractObjectSpecificExecutor<O1,O2,R> executor
      The executor to which to (re-)add this runnable if required (more work present, delay has elapsed, ...).
    • objects

      protected final Map<O2,Pair<ObjectRunnable<O1,O2>,R>> objects
      The data structures containing all objects and the corresponding work.
    • terminate

      protected final AtomicInteger 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 via objects.
    • logger

      protected final Logger 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.
      Use AbstractObjectRunnable(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

      public O1 getRegisteredObject()
      Description copied from interface: ObjectRunnable
      Gets the object with which this runnable has been registered at an object-specific executor service.
      Specified by:
      getRegisteredObject in interface ObjectRunnable<O1,O2>
      Returns:
      The object with which this runnable has been registered at an object-specific executor service.
    • getIndexObject

      public O2 getIndexObject()
      Description copied from interface: ObjectRunnable
      Gets the object with which this runnable is managed and executed within an object-specific executor service.
      Specified by:
      getIndexObject in interface ObjectRunnable<O1,O2>
      Returns:
      The object with which this runnable is managed and executed within an object-specific executor service.
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • submitted

      public void submitted()
      Description copied from interface: ManagedRunnable
      Signals 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:
      submitted in interface ManagedRunnable
    • rejected

      public void rejected()
      Description copied from interface: ManagedRunnable
      Signals that submitting an execution of this runnable has failed, that is, the pool has rejected it. This reverts ManagedRunnable.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:
      rejected in interface ManagedRunnable
    • 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 on objects themselves; 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 return false if 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

      public void awaitTermination() throws InterruptedException
      Description copied from interface: ObjectRunnable
      Awaits 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:
      awaitTermination in interface ObjectRunnable<O1,O2>
      Throws:
      InterruptedException - If the waiting thread is interrupted while waiting for this runnable to terminate, an InterruptedException will be thrown.