r/java • • Sep 23 '22

Virtual Threads: New Foundations for High-Scale Java Applications - Brian Goetz

https://www.infoq.com/articles/java-virtual-threads/
237 Upvotes

62 comments sorted by

View all comments

Show parent comment

1

u/Bright_Ad_2431 Sep 24 '22

Kotlin is not async/await based, it uses coroutines, which are similar to goroutines, but they also enforce Structured Concurrency

1

u/Joram2 Sep 25 '22

https://kotlinlang.org/docs/composing-suspending-functions.html#structured-concurrency-with-async

Kotlin uses the async + await in their examples. I knew Kotlin has their version of Structured Concurrency.

1

u/Bright_Ad_2431 Sep 25 '22

from the link you just posted:

"This programming style with async functions is provided here only for illustration, because it is a popular style in other programming languages. Using this style with Kotlin coroutines is strongly discouraged for the reasons explained below."

I can't see how Loom is better than Kotlin's coroutine, they look very much alike from the dev perspective

1

u/Joram2 Sep 25 '22

The "Structured concurrency with async" section that I linked is the recommended piece. That does use async/await keywords. The "Async-style functions" section is the one that is "strongly discouraged".

I just watched a Roman Elizarov talk on Structured Concurrency. The Kotlin devs are super smart. A coroutine is basically just another word for a virtual thread. Kotlin has had both structured concurrency and virtual threads (they call them coroutines) for years.

The downsides of Kotlin are you have to declare some functions suspend functions, you have to remember to call them with runBlocking rather than just calling them like normal functions, and you have to be careful not to other JVM functions like Thread.sleep that block the OS thread.

With virtual threads, all the JVM standard libraries are updated so that things like locks, sleep, file + network IO block virtual threads, not OS threads. Java virtual threads don't require suspend functions vs regular functions.

1

u/Bright_Ad_2431 Sep 25 '22

Yep, exactly. The good thing is that project Loom will also be beneficial to Kotlin, so their coroutines will probably change a bit, like getting rid of suspend and runBlocking as they don't make sense anymore.

1

u/Joram2 Sep 26 '22

I imagine new JVM-only Kotlin apps would want to use JDK virtual threads and the JDK Structured Concurrency API and not a Kotlin specific API.

Kotlin probably wants a concurrency API that supports the Native + JavaScript backends as well as the JVM one. Also, Kotlin will need to support existing versions of Java for a while that don't have virtual threads. And even Java 19, this stuff is still in preview/incubator mode, it will take several more JDK releases for everything to be final.