bissell
05/31/2018, 6:38 AM/**
* No locks are used to synchronize an access to the [Lazy] instance value; if the instance is accessed from multiple threads, its behavior is undefined.
*
* This mode should not be used unless the [Lazy] instance is guaranteed never to be initialized from more than one thread.
*/
Let's say my lazy
field is calculated from val
inputs on a data class by a deterministic, pure function. I don't mind potentially duplicating the work to calculate it in multiple threads, and would rather elide any synchronization costs. Is there any harm to using NONE
in this case? Maybe some weirdness around Kotlin Native I haven't considered?bissell
05/31/2018, 6:51 AMLazyThreadSafetyMode
, it should be possible to initialize the value into a final
field and lean on the Java Memory Model to handle the memory barriers. (https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.5) But again maybe this falls apart in native contextsspand
05/31/2018, 7:32 AMspand
05/31/2018, 7:35 AMilya.gorbunov
05/31/2018, 11:37 AMIs there any harm to usingYes, you may get NPE when accessing the value concurrently.in this case?NONE
I don't mind potentially duplicating the work to calculate it in multiple threads, and would rather elide any synchronization costs.The
PUBLICATION
mode is the best choice for that.ilya.gorbunov
05/31/2018, 11:38 AM