No. But final is better than volatile. Hotspot will consider it more
constant. And reading it doesn't require a barrier (some in this list might
add a little precision on that because final is not fully final but I will
ignore that for now) since we know it's the right value from the start.

The bottom line is: Always put final when you can on a class attribute (we
don't care about parameters and variables being final. I just consider it
to be visual noise)

On Thu, 27 Aug 2020 at 10:08, SuperNunrg <[email protected]> wrote:

> MemoizingSupplier is keeping the delegate forever.
>> NonSerializableMemoizingSupplier is releasing the delegate (delegate =
>> null) when the value is captured because it's not needed anymore.
>
>
> yes, that's pretty clear from the code.
>
> That's why one is final and the other volatile.
>
>
> and this one is not.
>
> For me it seems that final serves the purpose of safe initialization in
> case that another thread which is actually "initializing"(calls get()
> method on an instance of a MemozingSupplier) could properly see a delegate.
> And in case of NonSerializableMemoizingSupplier volatile seems to do the
> same thing. Does final adds any special properties over volatile than an
> object is serialized?
>
>
>
> On Thu, Aug 27, 2020 at 3:58 PM Henri Tremblay <[email protected]>
> wrote:
>
>> MemoizingSupplier is keeping the delegate forever.
>> NonSerializableMemoizingSupplier is releasing the delegate (delegate =
>> null) when the value is captured because it's not needed anymore.
>>
>> That's why one is final and the other volatile.
>>
>> Why they are not behaving the same way is a good question.
>>
>> On Thu, 27 Aug 2020 at 07:18, SuperNunrg <[email protected]> wrote:
>>
>>>
>>> Thank you all for answers. Let me ask one more question here.
>>> Guava has a lot of useful classes such as
>>>
>>> https://github.com/google/guava/blob/master/guava/src/com/google/common/base/Suppliers.java
>>> there are two classes MemoizingSupplier and
>>> NonSerializableMemoizingSupplier
>>> the first one has delegate marked as final and second as volotile. Why
>>> is that? could NonSerializableMemoizingSupplier has its delegate marked as
>>> final?
>>> has it something to do with GC?
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "mechanical-sympathy" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To view this discussion on the web, visit
>>> https://groups.google.com/d/msgid/mechanical-sympathy/69a4dd6c-8f15-47b7-936c-17761b481df6o%40googlegroups.com
>>> <https://groups.google.com/d/msgid/mechanical-sympathy/69a4dd6c-8f15-47b7-936c-17761b481df6o%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "mechanical-sympathy" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To view this discussion on the web, visit
>> https://groups.google.com/d/msgid/mechanical-sympathy/CADZL2%3DtjuFFn9pPMBApmG8dVZ5CNNNetEXogZ1H9%2BjyuUU9%2B3w%40mail.gmail.com
>> <https://groups.google.com/d/msgid/mechanical-sympathy/CADZL2%3DtjuFFn9pPMBApmG8dVZ5CNNNetEXogZ1H9%2BjyuUU9%2B3w%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "mechanical-sympathy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/mechanical-sympathy/CAJ183RNmFDBYA3HqOjrwHHiiQJ-OAbKc1x8YrZ1n9uMYnkSHVg%40mail.gmail.com
> <https://groups.google.com/d/msgid/mechanical-sympathy/CAJ183RNmFDBYA3HqOjrwHHiiQJ-OAbKc1x8YrZ1n9uMYnkSHVg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web, visit 
https://groups.google.com/d/msgid/mechanical-sympathy/CADZL2%3Ds6vJbBVu6GsR5MAPdz857ysOvC%3DDg-mfz6R01MyQSgPA%40mail.gmail.com.

Reply via email to