Comments on: Virtual Threads in Java – Deep Dive with Examples https://www.happycoders.eu/java/virtual-threads/ Wed, 09 Apr 2025 10:21:03 +0000 hourly 1 By: Sven Woltmann https://www.happycoders.eu/java/virtual-threads/#comment-19944 Wed, 25 Oct 2023 17:46:30 +0000 https://www.happycoders.eu/books/virtual-threads/#comment-19944 In reply to Arnaud.

Hi Arnaud,

The point is that platform threads are limited (not to 100, but let's say to 10,000). At some point they will make your operating system unstable and crash your computer (you can find several presentations on virtual threads where the presenters crashed their computers trying to figure out how many platform threads they can start).

Let's say we don't have 1,000 tasks, but 1,000,000.

Then we cannot use `newCachedThreadPool()` or `newThreadPerTaskExecutor(...)` - because trying to create 1,000,000 threads will pretty quickly crash your computer. So let's use `newFixedThreadPool(10_000)` to be safe.

Running 1,000,000 tasks on a pool of 10,000 platform threads will take 100 seconds.

But running the same amount of tasks on a `newVirtualThreadPerTaskExecutor()` will only take a bit more than a second - maybe 2 or 3 seconds with all the overhead - but far less than 100 seconds.

Best wishes
Sven

]]>
By: Arnaud https://www.happycoders.eu/java/virtual-threads/#comment-19930 Wed, 25 Oct 2023 10:50:27 +0000 https://www.happycoders.eu/books/virtual-threads/#comment-19930 If I say "start 100 tasks at a time that wait 1 second", it's not the same as "start all tasks simultaneously that wait 1 second". Yet, that is what you compare. Of course the first will take around "tasks/100" seconds while the other takes slightly more than a second. It's a very misleading comparison IMHO. At least using `newCachedThreadPool()` or `newThreadPerTaskExecutor(...)` might make the comparison more worthwhile.

]]>
By: Sven Woltmann https://www.happycoders.eu/java/virtual-threads/#comment-19756 Thu, 19 Oct 2023 17:12:07 +0000 https://www.happycoders.eu/books/virtual-threads/#comment-19756 In reply to paulsofts.

A) They allow you to write code in the traditional blocking "thread-per-request" manner. This is familiar to any developer and is easily maintained even by developers who are not pros at asynchronous programming. Also, code written this way does not need to be rewritten to asynchronous code in order to scale.

B) The program is easier to profile and debug because on the stack, you see your code, and not mainly the asynchronous framework's code.

]]>
By: paulsofts https://www.happycoders.eu/java/virtual-threads/#comment-19755 Thu, 19 Oct 2023 16:31:10 +0000 https://www.happycoders.eu/books/virtual-threads/#comment-19755 How can virtual threads be more helpful than asynchronous calls?

]]>
By: Jani Poikela https://www.happycoders.eu/java/virtual-threads/#comment-17903 Tue, 30 May 2023 10:19:37 +0000 https://www.happycoders.eu/books/virtual-threads/#comment-17903 This looks very promising.

]]>