Package org.springframework.core.task

# Class VirtualThreadTaskExecutor

java.lang.Object
org.springframework.core.task.VirtualThreadTaskExecutor

All Implemented Interfaces:
  `Executor`, `AsyncTaskExecutor`, `TaskExecutor`

public class VirtualThreadTaskExecutor extends Object implements AsyncTaskExecutor
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(boolean)` instead.

Since:
  6.1
Author:
  Juergen Hoeller
See Also:
  - `SimpleAsyncTaskExecutor.setVirtualThreads(boolean)`

## Field Summary

### Fields inherited from interface org.springframework.core.task.AsyncTaskExecutor

`TIMEOUT_IMMEDIATE, TIMEOUT_INDEFINITE`

## Constructor Summary

Constructors
Constructor
Description
`VirtualThreadTaskExecutor()`
Create a new `VirtualThreadTaskExecutor` without thread naming.
`VirtualThreadTaskExecutor(String threadNamePrefix)`
Create a new `VirtualThreadTaskExecutor` with thread names based on the given thread name prefix followed by a counter (for example, "test-0").

## Method Summary

Modifier and Type
Method
Description
`void`
`execute(Runnable task)`
Execute the given `task`.
`final ThreadFactory`
`getVirtualThreadFactory()`
Return the underlying virtual `ThreadFactory`.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface org.springframework.core.task.AsyncTaskExecutor

`execute, submit, submit, submitCompletable, submitCompletable`

## Constructor Details

### VirtualThreadTaskExecutor

public VirtualThreadTaskExecutor()
Create a new `VirtualThreadTaskExecutor` without thread naming.

### VirtualThreadTaskExecutor

public VirtualThreadTaskExecutor(String threadNamePrefix)
Create a new `VirtualThreadTaskExecutor` with thread names based on the given thread name prefix followed by a counter (for example, "test-0").

Parameters:
  `threadNamePrefix` - the prefix for thread names (for example, "test-")

## Method Details

### getVirtualThreadFactory

public final ThreadFactory getVirtualThreadFactory()
Return the underlying virtual `ThreadFactory`. Can also be used for custom thread creation elsewhere.

### execute

public void execute(Runnable task)
Description copied from interface: `TaskExecutor`
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.

Specified by:
  `execute` in interface `Executor`
Specified by:
  `execute` in interface `TaskExecutor`
Parameters:
  `task` - the `Runnable` to execute (never `null`)