Top Description Fields Constructors Methods
org.springframework.core.task

public Class VirtualThreadTaskExecutor

extends Object
implements AsyncTaskExecutor
Class Inheritance
All Implemented Interfaces
org.springframework.core.task.AsyncTaskExecutor, org.springframework.core.task.TaskExecutor, java.util.concurrent.Executor
Imports
java.util.concurrent.ThreadFactory

A TaskExecutor implementation based on virtual threads in JDK 21+. The only configuration option is a thread name prefix.

For additional features such as concurrency limiting or task decoration, consider using SimpleAsyncTaskExecutor#setVirtualThreads instead.

Author
Juergen Hoeller
Since
6.1
See Also
SimpleAsyncTaskExecutor#setVirtualThreads

Field Summary

Modifier and TypeField and Description
private final ThreadFactory

Constructor Summary

AccessConstructor and Description
public
VirtualThreadTaskExecutor()

Create a new VirtualThreadTaskExecutor without thread naming.

public
VirtualThreadTaskExecutor(String
the prefix for thread names (e.g. "test-")
threadNamePrefix
)

Create a new VirtualThreadTaskExecutor with thread names based on the given thread name prefix followed by a counter (e.g. "test-0").

Method Summary

Modifier and TypeMethod and Description
public void
execute(Runnable
the Runnable to execute (never null)
task
)

Implements org.springframework.core.task.TaskExecutor.execute.

Execute the given task.

public final ThreadFactory
getVirtualThreadFactory()

Return the underlying virtual ThreadFactory.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

virtualThreadFactoryback to summary
private final ThreadFactory virtualThreadFactory

Constructor Detail

VirtualThreadTaskExecutorback to summary
public VirtualThreadTaskExecutor()

Create a new VirtualThreadTaskExecutor without thread naming.

VirtualThreadTaskExecutorback to summary
public VirtualThreadTaskExecutor(String threadNamePrefix)

Create a new VirtualThreadTaskExecutor with thread names based on the given thread name prefix followed by a counter (e.g. "test-0").

Parameters
threadNamePrefix:String

the prefix for thread names (e.g. "test-")

Method Detail

executeback to summary
public void execute(Runnable task)

Implements org.springframework.core.task.TaskExecutor.execute.

Doc from org.springframework.core.task.TaskExecutor.execute.

Execute the given task.

The call might return immediately if the implementation uses an asynchronous execution strategy, or might block in the case of synchronous execution.

Parameters
task:Runnable

the Runnable to execute (never null)

Annotations
@Override
getVirtualThreadFactoryback to summary
public final ThreadFactory getVirtualThreadFactory()

Return the underlying virtual ThreadFactory. Can also be used for custom thread creation elsewhere.