Contents

How many Virtual Threads run in Parallel

Writen by: David Vlijmincx

Introduction

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.

Setup

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.

Answer

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.

The code

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 thread.sleep(), 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public class HowDoVirtualThreadsRun {

    static AtomicInteger atomicInteger = new AtomicInteger();

    public static void main(String[] args) {
        System.out.println("Core count is: " + Runtime.getRuntime().availableProcessors());

        try (ExecutorService e = Executors.newVirtualThreadPerTaskExecutor()) {
            for (int i = 0; i != 17; i++) {
                int finalI = i;
                e.submit(() -> run(finalI));
            }
        }
    }

    public static void run(int number) {
        for (int i = 0; i < 1600000; i++) {}
        System.out.println("Done runnable " + number);
    }

}

Further reading

More about virtual threads in Java:

 




Questions, comments, concerns?

Have a question or comment about the content? Feel free to reach out!