4 ms·
What patterns does async/await solve which virtual threads don’t?
by he0001 2y ago
What patterns does async/await solve which virtual threads don’t?
- neonsunset 2y ago"Green Threads" as implemented in Java is a solution that solves only a single problem - blocking/multiplexing. It does not enable easy concurrency and task/future composition the way C#/JS/Rust do, which offer strictly better and more comprehensive model.
- za3faran 2y agoStructured concurrency[1] offers task composition and more. [1] https://openjdk.org/jeps/453 https://openjdk.org/jeps/453
- he0001 2y agoWhat do you mean? It implements the Future/Task interface and you can definitely use that. In fact you can’t tell the difference from a virtual thread vs a platform one, and it’s available everywhere. I for one thinks it’s much easier to use than the async/await pattern as I don’t need any special syntax to use it.
- jayd16 2y agoIf you need to be explicit about thread contexts because you're using a thread that's bound to some other runtime (say, a GL Context) or you simply want to use a single thread for synchronization like is common in UI programming with a Main/UI Thread, async/await does quite well. The async/await sugar ends up being a better devx than thread locking and implicit threading just doesn't cut it. In Java they're working on a structured concurrency library to bridge this gap, but IMO, it'll end up looking like async/await with all its ups and downs but with less sugar.
- he0001 2y agoWhat’s stopping you from using a single thread for synchronization?
- jayd16 2y agoYou can use virtual threads running on a single OS thread and that will work but then everything will be on that one thread. You'll have synchronization but you'll also always be blocking on that one thread as well. Async/await is able to achieve good UX around explicitly defining what goes on your Main thread and what goes elsewhere. Its trivial to mix UI thread and background thread code by bouncing between synchronization contexts as needed. When the threading model is implicit its impossible to have this control.