How many platform threads are used to schedule virtual threads
Asked Answered
L

1

5

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.

Lenora answered 22/12, 2023 at 14:52 Comment(16)
How can this be answered if you don't specify a platform?Tallbott
Check my edit but I really don't understand the negative vote.Lenora
Also does it really depend on the platform?Lenora
Typically, as many platform threads as there are CPUs/cores, as that’s the most useful setting. See the documentation of ThreadConsueloconsuetude
You can contrive a platform that does not support threads but has the full API available and does things on an internal scheduler, so yes. In that case it would "effectively 1" and in other operating systems it would be "possibly more than one"Tallbott
@Consueloconsuetude I kind of expected that, but is there no way to specify a pool threads?Lenora
What do you mean with “to specify a pool threads”?Consueloconsuetude
Lets say I want to create and specify sum x number of threads to be used as the base pool to schedule my virtual threads. Is there not an option? Basically different kind of tasks could have different type of io:cpu time split. In such case I might want to change the number of platform threads available. Will all the virtually threads in a process space use the same pool of platform threads?Lenora
You can test with different configurations in your environment, with your application/code, with your data, and find what values are more appropriate to reach your objectives. Note that we don't have any of those to try ourselves, nor we should do it for you.Eructate
Thats what I want to know, is there a way to tell how many platform threads I want to create for my virtual threads. Lets say I have a number I want to try. But there doesn't seem to be any API for thisLenora
The whole point of virtual threads is that they release the platform thread on I/O so the platform thread is free to process the next virtual thread. You can have one million virtual threads performing I/O at the same time, without blocking a platform thread. And CPU bound operations do not get faster when having more threads than CPU cores. So, there is no reason to change the number of platform threads. Besides that, I already linked to the (only) option for changing the number of platform threads.Consueloconsuetude
>CPU bound operations do not get faster when having more threads than CPU core True, but I could set priority on a pool of threads. so if I set different priority for some tasks which are more important latency wise, they should be scheduled more frequently on the CPU by the OS That means there still a value in specifying a threadpool for your virtual threadsLenora
The priority has far less impact than you think. Besides that, the answer is that there is no feature to specify a thread pool.Consueloconsuetude
To also keep in consideration How many threads can a Java VM support?Aggravation
If virtual threads' priorities is your concern, then, as of JDK 21.0.1, according to Thread.setPriority Javadoc: The priority of a virtual thread is always {@link Thread#NORM_PRIORITY} and {@code newPriority} is ignored. However, keeping in mind that carrier threads may also have their own priorities, it deserves to be a good separate question. In general, your question is good. Upvoted.Curtice
I am going deep and will ask several questions on the virtual thread.Lenora
L
9

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.
Limitation answered 22/12, 2023 at 16:13 Comment(2)
Thanks a lot! So I guess there is no method to take ThreadFactory too? I would like to name my threads for observability and maybe make then daemon.Lenora
You can use, as of JDK 21.0.1, Executors.newThreadPerTaskExecutor instead and pass to it as a parameter your wrapper/delegator of Thread.ofVirtual().factory() and set a name in the newThread method. Note, however, that virtual threads are always daemons according to Thread.isDaemon() Javadoc: The daemon status of a virtual thread is always {@code true}.Curtice

© 2022 - 2025 — McMap. All rights reserved.