Is it safe to use AtomicLong inside coroutines? I have the following code:
Copy code
fun a() {
Log.i("Log info")
launch {
myAtomicLong.set(SystemClock.elapsedRealtime())
Log.i("Log info in launch")
}
}
The second Log.i never gets executed. It’s inside a class implementing coroutineScope on Dispatchers.IO. The AtomicLong is used in multiple of those launch blocks.
m
Matej Drobnič
06/11/2019, 9:45 AM
Maybe scope is alrady cancelled? Does it execute if you comment out atomic call?
o
octylFractal
06/11/2019, 9:47 AM
as safe as in Java, Kotlin coroutines do not impose more restrictions to Java threads
m
maxmello
06/11/2019, 9:57 AM
I think you are correct about the cancelled scope, the job has a parent that might call cancelChildren() which then would make all the launches useless (🤦♂️ <- me)