Support runBlocking with arbitrary context
See original GitHub issueThe current behavior of runBlocking
with a specified context is somewhat low-level and hardly used in practice. In particular, unless the dispatcher that is passed to runBlocking
implements EventLoop
(usually - not), then runBlocking
creates its own private event loop and uses it as dispatcher, while ignoring the dispatcher that was passed to it in the context.
This proposal is to change this behavior so that runBlocking(UI) { ... }
would result in blocking the current thread and running the code in the context of UI
dispatcher, consistently with how other coroutine builders (launch
, async
, etc) operate. It looks that this is going to be less-surprising behavior.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Does runBlocking(Dispatchers.IO) block the main thread even ...
When CoroutineDispatcher is explicitly specified in the context, then the new coroutine runs in the context of the specified dispatcher ...
Read more >Guide to reactive streams with coroutines
Coroutine context to rule them all. A coroutine is always working in some context. For example, let us start a coroutine in the...
Read more >runBlocking in Kotlin Coroutines with Example - GeeksforGeeks
runBlocking is a coroutine function. By not providing any context, it will get run on the main thread.Runs a new coroutine and blocks...
Read more >Kotlin Coroutines 1.5: GlobalScope Marked as Delicate ...
If the network is slow, it keeps waiting in the background, consuming resources. ... fun main() = runBlocking<Unit> {
Read more >Kotlin Coroutines Concurrency - Kotlin Expertise Blog
Coroutines provide sophisticated means which help us structure code to ... The work on all tasks may be interleaved in some arbitrary order, ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Let me clarify the consistency here. For all other coroutine builders like
launch
, using them with a context:is equivalent to using them without context, but relying on
withContext
to change it:This change essentially brings the same behavior to
runBlocking
function.I think runBlocking + optional context is better, I don’t see why we should add one more function, Kotlin default arguments allow to avoid additional method without any drawbacks