suppoze
11/12/2018, 1:58 PMprivate val queue = Executors.newSingleThreadExecutor()
//...
fun submit(data: Data) {
queue.submit { updateData(data) }
}
? I was thinking about
private val queue = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
// ...
fun submit(data: Data) = runBlocking {
launch(queue) {
updateData(data)
}
}
but it's seems to be running sequentially somehow? Am I doing something wrong?marstran
11/12/2018, 2:01 PMrunBlocking
waits for the launch
to complete.marstran
11/12/2018, 2:01 PMGlobalScope.launch { updateData(data) }
.suppoze
11/12/2018, 2:08 PMlaunch
to complete. E.g. in this example https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/coroutine-context-and-dispatchers.md#unconfined-vs-confined-dispatcher
runBlocking
is used in conjuction launch
.gildor
11/12/2018, 2:13 PMmarstran
11/12/2018, 2:13 PMrunBlocking
creates a new CoroutineScope
. A CoroutineScope
will not complete before all child-coroutines complete. Look up "structured concurrency" 🙂gildor
11/12/2018, 2:14 PMgildor
11/12/2018, 2:14 PMsuppoze
11/12/2018, 2:19 PMgildor
11/13/2018, 5:02 AMupdateData
method mostly waits, so you can use async API instead of blocking