Gavin Ray
10/10/2022, 7:55 PMCompleteableFuture
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?andylamax
10/10/2022, 8:00 PMawait
suspend extension function, that works so well with coroutines. You should try that outGavin Ray
10/10/2022, 8:00 PM.await()
doesn't block, out of curiosity?andylamax
10/10/2022, 8:10 PMgildor
10/11/2022, 2:04 AMgildor
10/11/2022, 2:08 AMis there a way to get them to run on coroutines instead of physical threads to reduce the overheadTo 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
Gavin Ray
10/11/2022, 4:53 PMcoroutines 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 codeAhh okay, this clears up a lot, thank you! 🙏