Class CleanerRegistry

java.lang.Object
com.bc.ceres.util.CleanerRegistry

public final class CleanerRegistry extends Object
Central registry to (a) deterministically release resources via cleanup(owner), and (b) provide a GC fallback via Cleaner if close/cleanup was forgotten. Notes: - The fallback is NOT deterministic and does NOT help on hard process termination. - Cleanup actions must be idempotent (may run via cleanup(owner) or via Cleaner thread).
  • Method Details

    • getInstance

      public static CleanerRegistry getInstance()
    • register

      public Cleaner.Cleanable register(Object owner, CleanUpState cleanUpState)
      Registers a cleanup state for the given owner.

      The cleanup state may run either when cleanup(Object) is called explicitly or when the owner becomes unreachable and the cleaner performs fallback cleanup.

      The given CleanUpState must be a top-level class or a static nested class. It must not be a lambda, anonymous class, local class, or hold a direct reference to the owner.

      Cleanup implementations must be idempotent, because cleanup may be triggered deterministically via cleanup(Object) or nondeterministically by the cleaner.

      Parameters:
      owner - the object whose lifecycle is being tracked
      cleanUpState - the cleanup state to execute at most once
      Returns:
      the corresponding cleanable handle
      Throws:
      NullPointerException - if owner or cleanupState is null
      IllegalArgumentException - if cleanupState violates the registry constraints
    • cleanup

      public void cleanup(Object owner)
      Deterministically runs and deregisters all cleanup actions registered for the given owner. Safe to call multiple times.