Niklas Gürtler
01/29/2021, 10:59 AMval coroJob = AtomicReference<Job> ()
/// Start my coroutine, may be called from any thread
fun startCoro () {
val coro = coroutineScope.launch(<http://Dispatchers.IO|Dispatchers.IO>, start = LAZY) {
// Do the actual work here ...
coroJob.set(null)
}
// If coroJob isn't null, do nothing.
if (coroJob.compareAndSet(null, coro))
coro.start()
}
which avoids the explicit context switches and should still be thread-safe. Is there a more idiomatic and safe way to do that?uli
01/29/2021, 11:07 PMNiklas Gürtler
01/29/2021, 11:28 PMuli
01/29/2021, 11:32 PMNiklas Gürtler
02/01/2021, 7:56 AMuli
02/01/2021, 8:00 AMNiklas Gürtler
02/01/2021, 8:00 AMuli
02/01/2021, 9:50 AMuli
02/01/2021, 9:55 AMNEW
state.Niklas Gürtler
02/01/2021, 9:56 AMuli
02/01/2021, 9:56 AMNiklas Gürtler
02/01/2021, 9:57 AMuli
02/01/2021, 9:57 AMuli
02/01/2021, 9:57 AMNiklas Gürtler
02/01/2021, 9:57 AMelse coro.cancel()
would helpuli
02/01/2021, 10:06 AMval coroRunning = AtomicBoolean(false)
/// Start my coroutine, may be called from any thread
fun startCoro () {
// If coro is running, do nothing.
if (coroRunning.compareAndExchange(false, true)) {
coroutineScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
// Do the actual work here ...
coroRunning.set(false)
}
}
}
Niklas Gürtler
02/01/2021, 10:07 AMuli
02/01/2021, 10:08 AM