jay shah
06/16/2022, 11:18 AM?.let
not thread safe?gildor
06/17/2022, 9:27 AMgildor
06/17/2022, 9:28 AMgildor
06/17/2022, 9:28 AMgildor
06/17/2022, 9:30 AMEmanuel Moecklin
06/18/2022, 3:55 PMlet
creates a scope and the context object passed to the scope (that would be it
) is immutable but you're accessing a property outside the scope (INSTANCE
) and whatever is outside the scope might not be immutable. Change the code to
INSTANCE?.let { return it }
and the error goes awayDALDEI
06/26/2022, 4:59 PMDALDEI
06/26/2022, 5:00 PMDALDEI
06/26/2022, 5:01 PMgildor
06/27/2022, 2:36 AMhaving the early return OUTSIDE of syncronize there is an order of assigment race conditionI don’t see how it a problem in this case, I mean the fact that variable is checked outside of sycnronized, it’s completely valid optimisation as soon as INSTANCE is volatile to prevent reordering. But agree, missing null check inside of synchronized will allow creation of multiple instances So just adding additional null check inside of synchronized block will fix the problem (so will be standard double-checked lock) Lazy or Object would be great, but unfortunately, this is not standard singleton, this is singleton with parameter, which causes this issue. Instead of finding threading issues, I would rather restructure code and get rid of this anti-pattern completely, DI for help UPD: BTW, article which David shared shows that it valid solution with double check and volatile field (see @Fixing Double-Checked Locking using Volatile” section)
DALDEI
06/27/2022, 3:51 AM