Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration options for virtual threads #30241

Closed
jhoeller opened this issue Mar 30, 2023 · 3 comments
Closed

Configuration options for virtual threads #30241

jhoeller opened this issue Mar 30, 2023 · 3 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@jhoeller
Copy link
Contributor

jhoeller commented Mar 30, 2023

In addition to our general compatibility effort in #23443, we are going to explore first-class configuration options for virtual threads: e.g. a virtualThreads flag on SimpleAsyncTaskExecutor or a similar dedicated virtual-thread TaskExecutor implementation with thread naming and concurrency throttle support.

We may also introduce virtual thread flags in places where we currently use a SimpleAsyncTaskExecutor by default, such as in DefaultMessageListenerContainer. These facilities can then conveniently be configured for virtual threads on their own (not having to pass a custom ´Executor` for it) and possibly also as part of a wider-ranging virtual thread setup in Spring Boot 3.2.

In contrast to #23443, this effort will require some level of building against JDK 21, at least for the spring-core module which is likely to centralize the foundational configuration pieces for virtual threads. All other modules would just delegate to those classes/methods on an as-needed basis, without incurring any JDK 21 build requirements or defensive mechanisms for running on JDK 17-20 themselves.

@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement labels Mar 30, 2023
@jhoeller jhoeller added this to the 6.1.0-M1 milestone Mar 30, 2023
@jhoeller jhoeller self-assigned this Mar 30, 2023
@jhoeller
Copy link
Contributor Author

jhoeller commented May 5, 2023

A convenient setVirtualThreads(true) call on SimpleAsyncTaskExecutor turns out to be a straightforward option for bean-style configuration. The concurrency throttle and the thread naming in SimpleAsyncTaskExecutor is very applicable to virtual threads as well, so it makes sense to combine those capabilities.

In places where that common executor is used by default, it is easy enough to pass in a specifically configured SimpleAsyncTaskExecutor with a virtual thread setup already. A dedicated virtualThreads flag next to an existing taskExecutor property in such places seems a bit convoluted - next to all the non-thread-related configuration options that we have there already (e.g. on DefaultMessageListenerContainer).

In addition, a focused virtual-threads-based executor variant for programmatic use seems attractive as well, in particular for explicitly starting virtual threads from JDK 17 based code (in certain code paths) in other parts of the portfolio, without having to compile against JDK 21 in those places. A single such delegate at spring-core level with support for custom thread naming could be all that we need here.

poutsma added a commit that referenced this issue May 8, 2023
This commit makes sure that the SimpleAsyncTaskExecutor only creates a
virtual thread when the virtualThreadDelegate is set, and not an
additional kernel thread as well.

See gh-30241
@kmandalas
Copy link

@jhoeller based on https://openjdk.org/jeps/444 which recommends "Do not pool virtual threads", the new configuration options will be applicable only to SimpleAsyncTaskExecutor (and not the ThreadPoolTaskExecutor for example)?

@jhoeller
Copy link
Contributor Author

Indeed, those options will only be available on SimpleAsyncTaskExecutor and the new VirtualThreadTaskExecutor. We recommend to keep using platform threads for ThreadPoolTaskExecutor and ThreadPoolTaskScheduler, even if technically they can be configured with a virtual ThreadFactory.

mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
VirtualThreadDelegate built on JDK 21 for multi-release jar.
Includes dedicated VirtualThreadTaskExecutor as lean option.
Includes setVirtualThreads flag on SimpleAsyncTaskExecutor.
Includes additional default methods on AsyncTaskExecutor.

Closes spring-projectsgh-30241
mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
This commit makes sure that the SimpleAsyncTaskExecutor only creates a
virtual thread when the virtualThreadDelegate is set, and not an
additional kernel thread as well.

See spring-projectsgh-30241
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants