I have some basic questions about working with Cor...
# getting-started
g
I have some basic questions about working with Coroutines and JVM
CompleteableFuture
interop I'm not very familiar with JVM-style asynchrony (mostly used async/await style languages) If I have a Java API which returns `CompleteableFuture`'s, is there a way to get them to run on coroutines instead of physical threads to reduce the overhead, or how do you generally work with these?
a
You should ask in this #coroutines Also, If you have a third party that returns a CompletableFuture, there is an
await
suspend extension function, that works so well with coroutines. You should try that out
👍 1
g
.await()
doesn't block, out of curiosity?
a
It should not
👌 1
g
No, it doesn’t block The rule of thumb that any suspend API is not blocking, if it blocks in some situation (for example it does heavy computation without moving it to own dispatcher), it means that there is a bug and should be fixed/reported
is there a way to get them to run on coroutines instead of physical threads to reduce the overhead
To be honest, you cannot optimize it somehow, coroutines will help only if you replace CompletableFuture.get() with CompletableFuture.await(), because it allows you to avoid blocking thread, but it will not improve performance if you use CompletableFuture.whenComplete or similar async APIs, await() itself based on them, but it allows to write more readable and natural code, and if you already used to async/await in other languages, it should be easier for you to use coroutines anyway
g
coroutines will help only if you replace CompletableFuture.get() with CompletableFuture.await(), because it allows you to avoid blocking thread, but it will not improve performance if you use CompletableFuture.whenComplete or similar async APIs, await() itself based on them, but it allows to write more readable and natural code
Ahh okay, this clears up a lot, thank you! 🙏