3 ms·
> async/await allows you to do multiple things in parallel. I don't see how you can do that in the virtual threading model, although I haven't used it and only
by Jtsummers 4y ago
> async/await allows you to do multiple things in parallel. I don't see how you can do that in the virtual threading model, although I haven't used it and only read this article.
The description of this is that the virtual threads can move between platform threads, quoting from the article:
> The operating system only knows about platform threads, which remain the unit of scheduling. To run code in a virtual thread, the Java runtime arranges for it to run by mounting it on some platform thread, called a carrier thread. Mounting a virtual thread means temporarily copying the needed stack frames from the heap to the stack of the carrier thread, and borrowing the carriers stack while it is mounted.
> When code running in a virtual thread would otherwise block for IO, locking, or other resource availability, it can be unmounted from the carrier thread, and any modified stack frames copied are back to the heap, freeing the carrier thread for something else (such as running another virtual thread.) Nearly all blocking points in the JDK have been adapted so that when encountering a blocking operation on a virtual thread, the virtual thread is unmounted from its carrier instead of blocking.
This allows for parallelism so long as the system is multicore and the JVM has access to multiple parallel threads to distribute the virtual threads across.
- mgraczyk 4y agoTwo separate threads run in parallel, but one thread cannot do two subtasks in parallel without submitting parallel jobs to an executor or a StructuredTaskScope subtask manager. It's basically forcing the developer to do all the hard work and boilerplate that async/await saves you.
- pron 4y ago> It's basically forcing the developer to do all the hard work and boilerplate that async/await saves you. It doesn't. Both require the exact same kind of invocation by the user. Neither automatically parallelises operations that aren't explicitly marked for parallelisation.