0

Since java 9 we finally have Cleaner. However in the docs is specifically declared:

Each cleaner operates independently, managing the pending cleaning actions and handling threading and termination when the cleaner is no longer in use.

Registering an object reference and corresponding cleaning action returns a Cleanable. The most efficient use is to explicitly invoke the clean method when the object is closed or no longer needed.

The cleaning action is a Runnable to be invoked at most once when the object has become phantom reachable unless it has already been explicitly cleaned.

Note that the cleaning action must not refer to the object being registered. If so, the object will not become phantom reachable and the cleaning action will not be invoked automatically.

Unfortunately, this is exactly what I would need: the action I want to register needs to refer the object itself (usually it's nothing else then calling a method).

Since this looks not possible with the Cleaner, is there a way to do it using Phantom or weak/soft references?

I took a look at them, and they look fairly complicate, so before investing quite some time digging into them seriously, I'd like to know in advance if something like this is possible or not

Ps: apparently it looks like Phantom references now get executed post-mortem

Community
  • 1
  • 1
elect
  • 6,765
  • 10
  • 53
  • 119

0 Answers0