Contents

Configure virtual thread scheduler

Introduction

There are use cases where you want to limit the number of carrier threads that run simultaneously. You can limit the number of carrier threads in two ways. This post looks into two system properties you can set to achieve this.

Limit the number of carrier threads

The first property lets you set the number of carrier threads that are created for the virtual threads to use. By default, the number of carrier threads is the same as the number of available cores. To set the number of carrier threads created you can use the following property:

1
jdk.virtualThreadScheduler.parallelism=5

Limit the maximum number of carrier threads

The number of carrier threads can exceed the number set by the parallelism value (default is the number of cores) when virtual threads are blocked. These new carrier threads are created temporarily to accommodate the blocked virtual threads and carrier threads. To set the maximum amount of carrier threads that can be created, use the following system property:

1
jdk.virtualThreadScheduler.maxPoolSize=10

Conclusion

In conclusion, limiting the number of carrier threads that run simultaneously can be achieved through the use of two system properties in Java. The jdk.virtualThreadScheduler.parallelism property can be set to define the number of carrier threads created for virtual threads to use, while the jdk.virtualThreadScheduler.maxPoolSize property can be set to define the maximum number of carrier threads that can be created temporarily to accommodate blocked virtual threads.

References and further reading

Join the discussion: follow me on Twitter and share your thoughts

This post is just the beginning. If you're interested in learning more, be sure to follow me on Twitter . And if you have any questions or ideas, don't hesitate to reach out via Twitter or send me an email. I'm always happy to connect and continue the conversation.