RFR: 8197812: (ref) Data race in Finalizer

Peter Levart peter.levart at gmail.com
Wed Feb 14 10:49:00 UTC 2018

Hi Martin,

On 02/14/2018 10:58 AM, Peter Levart wrote:
> I take back this claim. Of course the the following race is possible:
> - Thread1: calls runAllFinalizers and takes a Finalizer from 
> 'unprocessed' list.
> - Thread2: takee the same Finalizer instance from ReferenceQueue and 
> calls runFinalizer()
> - Thread1: calls runFinalizer() with the same instance for the 2nd 
> time now.

... but this could be "fixed" if the taking of next Finalizer from 
'unfinalized' list and removing it from the same list was a single 
atomic operation. What do you say of the following further simplification:


Regards, Peter

> Regards, Peter
> On 02/14/2018 10:39 AM, Peter Levart wrote:
>> I could even claim that simplifying the if statement in remove() to:
>>     if (unfinalized == this) {
>>         unfinalized = this.next;
>>     }
>> makes checking for hasBeenFinalized() in runFinalizer() redundant as 
>> it would not be possible for runFinalizer() to be called more than 
>> once for each Finalizer instance because:
>> - ReferenceQueue never returns the same Reference instance twice or 
>> more times.
>> - 'unfinalized' will never point back to the same Finalizer instance 
>> for the 2nd time, because it always "travels" in the forward 
>> direction (unfinalized = unfinalized.next).
>> Regards, Peter
>> (I rest now).

More information about the core-libs-dev mailing list