Class 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 Detail

      • 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 Detail

      • 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 Detail

      • run

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