We can start a lot of virtual threads, like really a lot. But have you ever wondered how many can run at the same time?
In this article, we are going to find out how many virtual threads will run at the same time.
The idea is to start several virtual threads with the same task and see how many finish at the time. I keep increasing the number of virtual threads till one thread finishes later than the others. We could also read the documentation and find it there, but this is more fun.
The number of virtual threads that run simultaneously is the same number of cores available inside the system. This includes the Hyper-Threading/SMT cores on your Intel or AMD system. So if you have a 4-core CPU with Hyper-Threading/SMT, you can run 8 virtual threads simultaneously. You don't have to take my word for it, please see the documentation for a more detailed answer.
Please keep reading to find out how we came to this answer.
Let's find out how many virtual threads run at the same time! To do is, we need three things:
- A task that takes some time to finish
- An ExecutorService to start virtual threads
- An optional print method to make it more readable
The run method is used to keep the virtual threads busy for the same amount of time. We cannot use a
because another thread would start while another sleeps.
I use the
Executors.newVirtualThreadPerTaskExecutor to create a virtual thread for each task. A for loop inside the
try-with-resources is used to spawn the number of threads I want to test.
All that is left is to run the code and see if all your threads finish at the same time. If they do, increase the number of threads until one thread finishes later than the others. The number you end up with will be the same as the number of cores available.
More about virtual threads in Java: