WeakReference with null referent
mandy chung
mandy.chung at oracle.com
Tue Jul 10 23:26:48 UTC 2018
On 7/10/18 4:04 PM, Éamonn McManus wrote:
>> 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
Thanks Eamonn.
I also found a few other similar usages in JDK to initialize
with an "empty" WeakReference field that may be updated with
a weak ref with a referent at runtime so that the code can
always assume this field is non-null.
Due to the current usage and compatibility risk, not worth
to enforce non-null referent.
Mandy
> 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