org.springframework.core.task

Class SimpleAsyncTaskExecutor

  • All Implemented Interfaces:
    Serializable, Executor, AsyncTaskExecutor, TaskExecutor

    public class SimpleAsyncTaskExecutor
    extends CustomizableThreadCreator
    implements AsyncTaskExecutor, Serializable
    TaskExecutor implementation that fires up a new Thread for each task, executing it asynchronously.

    Supports limiting concurrent threads through the "concurrencyLimit" bean property. By default, the number of concurrent threads 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.

    Since:
    2.0
    Author:
    Juergen Hoeller
    See Also:
    setConcurrencyLimit(int), SyncTaskExecutor, org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor, org.springframework.scheduling.commonj.WorkManagerTaskExecutor, Serialized Form
    • Field Detail

      • UNBOUNDED_CONCURRENCY

        public static final int UNBOUNDED_CONCURRENCY
        Permit any number of concurrent invocations: that is, don't throttle concurrency.
        See Also:
        Constant Field Values
      • NO_CONCURRENCY

        public static final int NO_CONCURRENCY
        Switch concurrency 'off': that is, don't allow any concurrent invocations.
        See Also:
        Constant Field Values
    • Constructor Detail

      • 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.
        Parameters:
        threadNamePrefix - the prefix to use for the names of newly created threads
      • SimpleAsyncTaskExecutor

        public SimpleAsyncTaskExecutor(ThreadFactory threadFactory)
        Create a new SimpleAsyncTaskExecutor with the given external thread factory.
        Parameters:
        threadFactory - the factory to use for creating new Threads
    • Method Detail

      • getThreadFactory

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

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

        In principle, this limit can be changed at runtime, although it is generally designed as a config time setting. NOTE: Do not switch between -1 and any concrete limit at runtime, as this will lead to inconsistent concurrency counts: A limit of -1 effectively turns off concurrency counting completely.

        See Also:
        UNBOUNDED_CONCURRENCY
      • getConcurrencyLimit

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

        public final boolean isThrottleActive()
        Return whether this throttle is currently active.
        Returns:
        true if the concurrency limit for this instance is active
        See Also:
        getConcurrencyLimit(), setConcurrencyLimit(int)
      • execute

        public void execute(Runnable task)
        Executes the given task, within a concurrency throttle if configured (through the superclass's settings).
        Specified by:
        execute in interface  Executor
        Specified by:
        execute in interface  TaskExecutor
        Parameters:
        task - the Runnable to execute (never null)
        See Also:
        doExecute(Runnable)
      • submit

        public Future<?> submit(Runnable task)
        Description copied from interface: AsyncTaskExecutor
        Submit a Runnable task for execution, receiving a Future representing that task. The Future will return a null result upon completion.
        Specified by:
        submit in interface  AsyncTaskExecutor
        Parameters:
        task - the Runnable to execute (never null)
        Returns:
        a Future representing pending completion of the task
      • submit

        public <T> Future<T> submit(Callable<T> task)
        Description copied from interface: AsyncTaskExecutor
        Submit a Callable task for execution, receiving a Future representing that task. The Future will return the Callable's result upon completion.
        Specified by:
        submit in interface  AsyncTaskExecutor
        Parameters:
        task - the Callable to execute (never null)
        Returns:
        a Future representing pending completion of the task