Casey Brooks
04/12/2019, 3:38 PM@ThreadLocal
on the companion object and @SharedImmutable
on the value to be initialized once. But that @ThreadLocal
annotation makes me think that another thread might try to access the shared object and have it not be there, does the @SharedImmutable
mean that each thread-local companion object will reference the same API key after initialization? Here’s basically what I’ve got class SingletonConfiguration
private constructor(
val apiKey: String
) {
@ThreadLocal
companion object {
@SharedImmutable
lateinit var instance: SingletonConfiguration
fun init(apiKey: String) : SingletonConfiguration {
instance = SingletonConfiguration(apiKey)
return instance
}
}
}
Dominaezzz
04/12/2019, 3:42 PMAtomicReference
and discard the @ThreadLocal
.kpgalligan
04/12/2019, 3:42 PMkpgalligan
04/12/2019, 3:43 PMDominaezzz
04/12/2019, 3:43 PMkpgalligan
04/12/2019, 3:43 PMkpgalligan
04/12/2019, 3:45 PMCasey Brooks
04/12/2019, 3:45 PMAtomicReference
? It doesn’t seem to be in the common stdlib, and I can’t seem to find documentation on a particular artifact I need to use for nativekpgalligan
04/12/2019, 3:45 PMkpgalligan
04/12/2019, 3:45 PMkpgalligan
04/12/2019, 3:46 PMkpgalligan
04/12/2019, 3:46 PMkpgalligan
04/12/2019, 3:47 PMDominaezzz
04/12/2019, 3:48 PMkotlinx.atomicfu
?kpgalligan
04/12/2019, 3:49 PMkpgalligan
04/12/2019, 3:50 PMkpgalligan
04/12/2019, 3:51 PMkpgalligan
04/12/2019, 3:51 PMkpgalligan
04/12/2019, 3:51 PMkpgalligan
04/12/2019, 3:53 PMCasey Brooks
04/12/2019, 8:33 PMAtomicReference
. “Stranger Threads: Part 2” also really helped me understand this all better, in particular the sections about freezing objects and atomics. Thanks for the help, y’all!kpgalligan
04/12/2019, 8:34 PM