Class CleanupRunnable

java.lang.Object
de.aristaflow.adept2.util.threading.CleanupRunnable
All Implemented Interfaces:
Runnable

public class CleanupRunnable extends Object implements Runnable
A cleanup runnable executes a runnable (or several ones) and afterwards a special cleanup runnable finally after the first runnable. Problems occurring in the cleanup runnable will be logged to the designated logger (and added as suppressed to problems of the runnable if appropriate). This way exceptions from the first runnable will not be swallowed.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Runnable
    The cleanup runnable executed after the normal execution has terminated.
    protected final Logger
    The logger for logging exceptions from the cleanup runnable.
    protected final Runnable
    The runnable executed normally.
    protected final AtomicLong
    A count for concurrently executed runnables sharing a common resource cleaned up in cleanup.
  • Constructor Summary

    Constructors
    Constructor
    Description
    CleanupRunnable(Runnable runnable, Runnable cleanup, Logger logger)
    Creates a new cleanup runnable executing the first runnable and the cleanup runnable finally after the first runnable.
    CleanupRunnable(Runnable runnable, AtomicLong sharingCount, Runnable cleanup, Logger logger)
    Creates a new cleanup runnable executing the first runnable (and similar runnables concurrently) and the cleanup runnable finally right after the last of these runnables have terminated.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    run()
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • runnable

      protected final Runnable runnable
      The runnable executed normally.
    • sharingCount

      protected final AtomicLong sharingCount
      A count for concurrently executed runnables sharing a common resource cleaned up in cleanup. This allows to defer cleanup untill all sharing runnables have terminated.
    • cleanup

      protected final Runnable cleanup
      The cleanup runnable executed after the normal execution has terminated.
    • logger

      protected final Logger logger
      The logger for logging exceptions from the cleanup runnable.
  • Constructor Details

    • CleanupRunnable

      public CleanupRunnable(Runnable runnable, Runnable cleanup, Logger logger)
      Creates a new cleanup runnable executing the first runnable and the cleanup runnable finally after the first runnable. Problems occurring in the cleanup runnable will be logged to the designated logger.
      Parameters:
      runnable - The runnable executed normally.
      cleanup - The cleanup runnable executed after the normal execution has terminated.
      logger - The logger for logging exceptions from the cleanup runnable.
    • CleanupRunnable

      public CleanupRunnable(Runnable runnable, AtomicLong sharingCount, Runnable cleanup, Logger logger)
      Creates a new cleanup runnable executing the first runnable (and similar runnables concurrently) and the cleanup runnable finally right after the last of these runnables have terminated. Problems occurring in the cleanup runnable will be logged to the designated logger, RuntimeExceptions will be additionally added as suppressed to an exception of the first runnable if present.
      Parameters:
      runnable - The runnable executed normally.
      sharingCount - The count for synchronising similar runnables so that cleanup will not run before all runnables have terminated.
      cleanup - The cleanup runnable executed after the designated runnables (and similar ones) have terminated.
      logger - The logger for logging exceptions from the cleanup runnable.
  • Method Details

    • run

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