WeakReference with null referent

mandy chung mandy.chung at oracle.com
Tue Jul 10 17:11:05 UTC 2018



On 7/9/18 11:59 PM, Peter Levart wrote:

> 
> There are situations where being able to create a XxxReference without a 
> referent is useful. For example, imagine a doubly-linked list of 
> WeakReference subclasses - a special instance without a referent can be 
> used as the head of such list that never goes away and simplifies 
> insertion/deletion logic, etc...
> 
> JDK's java.lang.ref.Cleaner API uses such scheme in its implementation 
> (see jdk.internal.ref.CleanerImpl.[Phantom|Weak|Soft]CleanableRef no-arg 
> constructors).

Thanks for pointing this out.  It's used as a convenience and it's
not hard to handle insertion/deletion logic though.   Another
alternative may be to create an object as the strongly reachable 
referent.  I wonder if any existing code depends on null referent
like the cleaner case and the compatibility risk.  Do you know
any other example?

Mandy


More information about the core-libs-dev mailing list