Does `runBlocking` exist for the sole purpose of b...
# coroutines
i
Does
runBlocking
exist for the sole purpose of bridging non-blocking with blocking code? If I have a java application that depends on a Kotlin library, would I be able to have the library support coroutines by wrapping the API functions with
runBlocking
, or are there other gotchas I need to know so the java application can take advantage of asynchronous code?
s
You can use it in the top function (
main()
), as the entry point into your unit-tests (if your tests use coroutines) and, like you said, bridging non-blocking to blocking code. The last situation could occur because you need to solve this: Say you have an interface method that you need to implement. But this interface method is declared as a regular non suspendable function that needs to wait for a result. And the code you use to actually need to use to implement that function uses suspendable functions…. That is where you can use
runBlocking
If you don’t want to block, consider using
future { }
, that return JDK8's
CompletableFuture<T>
i
This is very helpful. Thank you!