类 SimpleAsyncTaskExecutor

java.lang.Object
cn.taketoday.util.CustomizableThreadCreator
cn.taketoday.core.task.SimpleAsyncTaskExecutor
所有已实现的接口:
AsyncListenableTaskExecutor, AsyncTaskExecutor, TaskExecutor, Serializable, AutoCloseable, Executor

public class SimpleAsyncTaskExecutor extends CustomizableThreadCreator implements AsyncListenableTaskExecutor, Serializable, AutoCloseable
TaskExecutor implementation that fires up a new Thread for each task, executing it asynchronously. Provides a virtual thread option on JDK 21.

Supports a graceful shutdown through setTaskTerminationTimeout(long), at the expense of task tracking overhead per execution thread at runtime. Supports limiting concurrent threads through setConcurrencyLimit(int). By default, the number of concurrent task executions is unlimited.

NOTE: This implementation does not reuse threads! Consider a thread-pooling TaskExecutor implementation instead, in particular for executing a large number of short-lived tasks. Alternatively, on JDK 21, consider setting setVirtualThreads(boolean) to true.

从以下版本开始:
4.0
作者:
Juergen Hoeller, Harry Yang
另请参阅:
  • 字段详细资料

  • 构造器详细资料

    • SimpleAsyncTaskExecutor

      public SimpleAsyncTaskExecutor()
      Create a new SimpleAsyncTaskExecutor with default thread name prefix.
    • SimpleAsyncTaskExecutor

      public SimpleAsyncTaskExecutor(String threadNamePrefix)
      Create a new SimpleAsyncTaskExecutor with the given thread name prefix.
      参数:
      threadNamePrefix - the prefix to use for the names of newly created threads
    • SimpleAsyncTaskExecutor

      public SimpleAsyncTaskExecutor(@Nullable ThreadFactory threadFactory)
      Create a new SimpleAsyncTaskExecutor with the given external thread factory.
      参数:
      threadFactory - the factory to use for creating new Threads
  • 方法详细资料

    • setVirtualThreads

      public void setVirtualThreads(boolean virtual)
      Switch this executor to virtual threads. Requires Java 21 or higher.

      The default is false, indicating platform threads. Set this flag to true in order to create virtual threads instead.

    • setThreadFactory

      public void setThreadFactory(@Nullable ThreadFactory threadFactory)
      Specify an external factory to use for creating new Threads, instead of relying on the local properties of this executor.

      You may specify an inner ThreadFactory bean or also a ThreadFactory reference obtained from JNDI (on a Jakarta EE server) or some other lookup mechanism.

      另请参阅:
    • getThreadFactory

      @Nullable public final ThreadFactory getThreadFactory()
      Return the external factory to use for creating new Threads, if any.
    • setTaskDecorator

      public final void setTaskDecorator(@Nullable TaskDecorator taskDecorator)
      Specify a custom TaskDecorator to be applied to any Runnable about to be executed.

      Note that such a decorator is not necessarily being applied to the user-supplied Runnable/Callable but rather to the actual execution callback (which may be a wrapper around the user-supplied task).

      The primary use case is to set some execution context around the task's invocation, or to provide some monitoring/statistics for task execution.

      NOTE: Exception handling in TaskDecorator implementations is limited to plain Runnable execution via execute calls. In case of #submit calls, the exposed Runnable will be a FutureTask which does not propagate any exceptions; you might have to cast it and call Future#get to evaluate exceptions.

    • setTaskTerminationTimeout

      public void setTaskTerminationTimeout(long timeout)
      Specify a timeout (in milliseconds) for task termination when closing this executor. The default is 0, not waiting for task termination at all.

      Note that a concrete >0 timeout specified here will lead to the wrapping of every submitted task into a task-tracking runnable which involves considerable overhead in case of a high number of tasks. However, for a modest level of submissions with longer-running tasks, this is feasible in order to arrive at a graceful shutdown.

      Note that SimpleAsyncTaskExecutor does not participate in a coordinated lifecycle stop but rather just awaits task termination on close().

      参数:
      timeout - the timeout in milliseconds
      另请参阅:
      • close()
      • cn.taketoday.scheduling.concurrent.ExecutorConfigurationSupport#setAwaitTerminationMillis
    • isActive

      public boolean isActive()
      Return whether this executor is still active, i.e. not closed yet, and therefore accepts further task submissions. Otherwise, it is either in the task termination phase or entirely shut down already.
      另请参阅:
    • setConcurrencyLimit

      public void setConcurrencyLimit(int concurrencyLimit)
      Set the maximum number of parallel task executions allowed. The default of -1 indicates no concurrency limit at all.

      This is the equivalent of a maximum pool size in a thread pool, preventing temporary overload of the thread management system.

      另请参阅:
      • #UNBOUNDED_CONCURRENCY
      • cn.taketoday.scheduling.concurrent.ThreadPoolTaskExecutor#setMaxPoolSize
    • getConcurrencyLimit

      public final int getConcurrencyLimit()
      Return the maximum number of parallel task executions allowed.
    • isThrottleActive

      public final boolean isThrottleActive()
      Return whether the concurrency throttle is currently active.
      返回:
      true if the concurrency limit for this instance is active
      另请参阅:
    • execute

      public void execute(Runnable task)
      Executes the given task, within a concurrency throttle if configured (through the superclass's settings).
      指定者:
      execute 在接口中 Executor
      指定者:
      execute 在接口中 TaskExecutor
      参数:
      task - the Runnable to execute (never null)
      另请参阅:
    • execute

      public void execute(Runnable task, long startTimeout)
      Executes the given task, within a concurrency throttle if configured (through the superclass's settings).

      Executes urgent tasks (with 'immediate' timeout) directly, bypassing the concurrency throttle (if active). All other tasks are subject to throttling.

      指定者:
      execute 在接口中 AsyncTaskExecutor
      参数:
      task - the Runnable to execute (never null)
      startTimeout - the time duration (milliseconds) within which the task is supposed to start. This is intended as a hint to the executor, allowing for preferred handling of immediate tasks. Typical values are AsyncTaskExecutor.TIMEOUT_IMMEDIATE or AsyncTaskExecutor.TIMEOUT_INDEFINITE (the default as used by TaskExecutor.execute(Runnable)).
      另请参阅:
    • submit

      public Future<?> submit(Runnable task)
      从接口复制的说明: AsyncTaskExecutor
      Submit a Runnable task for execution, receiving a Future representing that task. The Future will return a null result upon completion.

      this method comes with a default implementation that delegates to TaskExecutor.execute(Runnable).

      指定者:
      submit 在接口中 AsyncTaskExecutor
      参数:
      task - the Runnable to execute (never null)
      返回:
      a Future representing pending completion of the task
    • submit

      public <T> Future<T> submit(Callable<T> task)
      从接口复制的说明: AsyncTaskExecutor
      Submit a Callable task for execution, receiving a Future representing that task. The Future will return the Callable's result upon completion.

      this method comes with a default implementation that delegates to TaskExecutor.execute(Runnable).

      指定者:
      submit 在接口中 AsyncTaskExecutor
      参数:
      task - the Callable to execute (never null)
      返回:
      a Future representing pending completion of the task
    • submitListenable

      public ListenableFuture<?> submitListenable(Runnable task)
      从接口复制的说明: AsyncListenableTaskExecutor
      Submit a Runnable task for execution, receiving a ListenableFuture representing that task. The Future will return a null result upon completion.

      in favor of AsyncTaskExecutor.submitCompletable(Runnable)

      指定者:
      submitListenable 在接口中 AsyncListenableTaskExecutor
      参数:
      task - the Runnable to execute (never null)
      返回:
      a ListenableFuture representing pending completion of the task
    • submitListenable

      public <T> ListenableFuture<T> submitListenable(Callable<T> task)
      从接口复制的说明: AsyncListenableTaskExecutor
      Submit a Callable task for execution, receiving a ListenableFuture representing that task. The Future will return the Callable's result upon completion.

      in favor of AsyncTaskExecutor.submitCompletable(Callable)

      指定者:
      submitListenable 在接口中 AsyncListenableTaskExecutor
      参数:
      task - the Callable to execute (never null)
      返回:
      a ListenableFuture representing pending completion of the task
    • doExecute

      protected void doExecute(Runnable task)
      Template method for the actual execution of a task.

      The default implementation creates a new Thread and starts it.

      参数:
      task - the Runnable to execute
      另请参阅:
    • newThread

      protected Thread newThread(Runnable task)
      Create a new Thread for the given task.
      参数:
      task - the Runnable to create a Thread for
      返回:
      the new Thread instance
      另请参阅:
    • close

      public void close()
      This close methods tracks the termination of active threads if a concrete task termination timeout has been set. Otherwise, it is not necessary to close this executor.
      指定者:
      close 在接口中 AutoCloseable