Package com.bc.ceres.util
Class CleanerRegistry
java.lang.Object
com.bc.ceres.util.CleanerRegistry
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 Summary
Modifier and TypeMethodDescriptionvoidDeterministically runs and deregisters all cleanup actions registered for the given owner.static CleanerRegistryregister(Object owner, CleanUpState cleanUpState) Registers a cleanup state for the given owner.
-
Method Details
-
getInstance
-
register
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
CleanUpStatemust 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 trackedcleanUpState- the cleanup state to execute at most once- Returns:
- the corresponding cleanable handle
- Throws:
NullPointerException- ifownerorcleanupStateisnullIllegalArgumentException- ifcleanupStateviolates the registry constraints
-
cleanup
Deterministically runs and deregisters all cleanup actions registered for the given owner. Safe to call multiple times.
-