Class 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.

Juergen Hoeller
See Also:
  • 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 (e.g. "test-0").
      threadNamePrefix - the prefix for thread names (e.g. "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
      task - the Runnable to execute (never null)