Contents

Configure virtual thread scheduler

Written by: David Vlijmincx

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

The default value for the maximum pool size is 256. These properties let you set the maximum pool size for carrier threads.

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