George
03/14/2022, 10:14 AMinternal fun AtomicRef<MutableList<Pair<KClass<*>, KSerializer<*>>>>.getOrWait(): MutableList<Pair<KClass<*>, KSerializer<*>>> {
while (this.value == emptyList<MutableList<Pair<KClass<*>, KSerializer<*>>>>())
suspend {
delay(10)
}
return this.value
}
Can anyone point any docs or articles on this subject? thanks in advance!Joffrey
03/14/2022, 10:18 AMAtomicRef<T>
type? It doesn't look like it's supposed to represent something that will be set in the future. Deferred<T>
(if you're using coroutines) or other CompletableFuture-like types seem more appropriate for this, and then you would get this await()
functionality for freeGeorge
03/14/2022, 10:20 AMAdrijan Rogan
03/14/2022, 10:27 AMLazy
? It might fit your needs and is thread safe. I'm a bit confused about your use case, what do you actually want?George
03/14/2022, 11:17 AMinternal val jsonSubclasses: Deferred<MutableList<Pair<KClass<*>, KSerializer<*>>>> =
CompletableDeferred(mutableListOf())
and i add to it in the initialization many times
internal suspend fun <T : Any> registerJsonSubclass(subclass: KClass<T>, serializer: KSerializer<T>) {
jsonSubclasses.await().add(subclass to serializer)
}
So from what i see i think lazily doesnt suit me or correct me if im wrong. Though thanks for ur advice rogan!
Is it ok for me to delay the initialization code like this?
internal suspend fun Deferred<MutableList<Pair<KClass<*>, KSerializer<*>>>>.getOrWait(): MutableList<Pair<KClass<*>, KSerializer<*>>> {
while (this.await() == emptyList<Pair<KClass<*>, KSerializer<*>>>())
delay(10)
return this.await()
}
Or i create many problems after this?Adrijan Rogan
03/14/2022, 11:25 AMjsonSubclasses
variable?George
03/14/2022, 11:41 AMinternal val jsonPayloadModule: SerializersModule = SerializersModule {
polymorphic(Any::class) {
subclass(StringAsObjectSerializer)
subclass(Data::class)
}
}
My code is in a library and not on general code.Adrijan Rogan
03/14/2022, 11:45 AMNick Allen
03/14/2022, 9:39 PMMutableList
is not thread-safe, no matter what contains it. If it's in a thread-safe container, then that thread-safety only applies to manipulating the container (like updating your AtomicRef
to point at a new MutableList
). If you want thread safe access to the list, then you need the list itself to be thread safe.
What is calling getOrWait
? The code setting up the SerializerModule? The code adding KSerializers? Why is it waiting? Just waiting for the list to be non-empty seems like a poor check if you want to wait it to be fully populated and there can be more than one item. Perhaps there is some extra infrastructure needed so that the initialization can be known for sure before the consumer of that initialized data even gets to it.