Search code examples
javavirtual-threads

How many platform threads are used to schedule virtual threads


If I create an executor service of virtual threads

Executors.newVirtualThreadPerTaskExecutor()

I understand that each tasks is run over a virtual thread. These virtual threads are scheduled (hopefully interleaved) over platform threads.

How many platform threads are used here? I don't see any other API to specify the number of threads I want. I am running it on Macbook M1 Pro with Java 21.


Solution

  • There is no API to specify the number of threads used for virtual threads, but the documentation of Thread tells you how you change that numbers through system properties:

    System property Description
    jdk.virtualThreadScheduler.parallelism The number of platform threads available for scheduling virtual threads. It defaults to the number of available processors.
    jdk.virtualThreadScheduler.maxPoolSize The maximum number of platform threads available to the scheduler. It defaults to 256.