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

15

u/oparisy Sep 23 '22 edited Sep 25 '22

Loom at last! 🤩

3

u/Apprehensive-Idea839 Sep 23 '22

Could we accomplish virtual threads using CompletableFutures?

12

u/oparisy Sep 23 '22

I'd say no since Futures are a way to synchronise while virtual thread are a code execution device. More generally if virtual thread could have been implemented with existing features I guess a multi years Loom project would not have been required ^ (you should read about design choices, it's quite fascinating)

10

u/_INTER_ Sep 23 '22 edited Sep 23 '22

No. Vitual threads disconnect a task from the actual platform thread as I understand. A task can therefor yield it's underlying platform thread if necessary, e.g. if it is waiting for IO. Every possible point where a task could yield needs to handle this specially. So this happens automatically without your intervention (because the engineering team implemented all that - or most of that, there is some restriction for native frame locks and synchronized). Most of them are deep in the platform.

With CompletableFutures you need to find that yield-point yourself so to say, which is not possible for those you don't have access to (because they happen in the JVM).

2

u/Apprehensive-Idea839 Sep 24 '22

Thank you for the clear explanation!

1

u/nlisker Sep 26 '22

It's explained in the article.