weak vs phantom references

Michał Zegan webczat_200 at poczta.onet.pl
Fri Nov 2 23:15:27 UTC 2018

Something that is not clear to me. If some object is referenced to by a
weak reference, it means it is not yet finalized, in the case of objects
being phantom reachable, they may be finalized but are still not garbage
My question is: if finalization wouldn't be used and you had a guarantee
that it isn't used for the given object, could weak references be used
in place of phantom references for cleanup purposes?
I know that phantom references must be registered with a ref queue and
you cannot get the referent using their get() method, but weak
references can also be registered into a queue, and in any case, when
any kind of reference lands in a queue, it is guaranteed that it is
already cleared, so if you use weak references for cleanup by polling
them from the queue, you also cannot access the referent.
Not sure if I understand it right...

More information about the jdk-dev mailing list