Hi :wave: I was trying to run a new coroutine on a...
# coroutines
j
Hi 👋 I was trying to run a new coroutine on a new thread context on Android by calling:
Copy code
newSingleThreadContext("MyName").use {
  runBlocking(it) {
    // some suspend calls, including `delay`
  }
}
but it always ended up with the ANR error. Is that expected? I thought that
runBlocking
blocked the current thread, which in this case should be a new background thread, not the main thread. If that’s the case, then why this code would result in ANR? Slack Conversation
s
runBlocking
always blocks the current thread, while it waits for its child coroutines to complete. The context you pass in can change which thread the coroutines are running on, but it doesn't change which thread is waiting for them. If you don't want the current thread to wait for the coroutines to complete, maybe you want to make a coroutine scope with your new context and use
launch
instead of
runBlocking
.
âž• 1
j
Ah, I see! For some reason I was fixed on the idea that
runBlocking
also runs on the new thread, but now it all makes sense 😅 thanks!