WeakReference with null referent

Éamonn McManus emcmanus at google.com
Tue Jul 10 23:04:54 UTC 2018


> Do you know any other example?

Here's an example from the JDK itself:
http://hg.openjdk.java.net/jdk/jdk11/file/2a89e80301b1/src/java.desktop/share/classes/sun/font/Type1Font.java#l101

On Tue, 10 Jul 2018 at 10:11, mandy chung <mandy.chung at oracle.com> wrote:
>
>
>
> 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