The Spring Framework

org.springframework.core.task
Class SimpleAsyncTaskExecutor

java.lang.Object
  extended by org.springframework.util.ConcurrencyThrottleSupport
      extended by org.springframework.core.task.SimpleAsyncTaskExecutor
All Implemented Interfaces:
Serializable, TaskExecutor

public class SimpleAsyncTaskExecutor
extends ConcurrencyThrottleSupport
implements TaskExecutor, 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: 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:
ConcurrencyThrottleSupport.setConcurrencyLimit(int), SyncTaskExecutor, TimerTaskExecutor, ThreadPoolTaskExecutor, WorkManagerTaskExecutor, Serialized Form

Field Summary
static String DEFAULT_THREAD_NAME_PREFIX
          Default thread name prefix: "SimpleAsyncTaskExecutor-".
 
Fields inherited from class org.springframework.util.ConcurrencyThrottleSupport
logger, NO_CONCURRENCY, UNBOUNDED_CONCURRENCY
 
Constructor Summary
SimpleAsyncTaskExecutor()
          Create a new SimpleAsyncTaskExecutor with default thread name prefix.
SimpleAsyncTaskExecutor(String threadNamePrefix)
          Create a new SimpleAsyncTaskExecutor with the given thread name prefix.
 
Method Summary
protected  Thread createThread(Runnable task)
          Template method for creation of a Thread.
protected  void doExecute(Runnable task)
          Template method for the actual execution of a task.
 void execute(Runnable task)
          Executes the given task, within a concurrency throttle if configured (through the superclass's settings).
protected  String getThreadNamePrefix()
          Return the thread name prefix to use for the names of newly created threads.
protected  int getThreadPriority()
          Return the priority of the threads that this executor creates.
protected  boolean isDaemon()
          Return whether this executor should create daemon threads.
protected  String nextThreadName()
          Return the thread name to use for a newly created thread.
 void setDaemon(boolean daemon)
          Set whether this executor should create daemon threads, just executing as long as the application itself is running.
 void setThreadNamePrefix(String threadNamePrefix)
          Specify the prefix to use for the names of newly created threads.
 void setThreadPriority(int threadPriority)
          Set the priority of the threads that this executor creates.
 
Methods inherited from class org.springframework.util.ConcurrencyThrottleSupport
afterAccess, beforeAccess, getConcurrencyLimit, isThrottleActive, setConcurrencyLimit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_THREAD_NAME_PREFIX

public static final String DEFAULT_THREAD_NAME_PREFIX
Default thread name prefix: "SimpleAsyncTaskExecutor-".

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
Method Detail

setThreadNamePrefix

public void setThreadNamePrefix(String threadNamePrefix)
Specify the prefix to use for the names of newly created threads. Default is "SimpleAsyncTaskExecutor-".


getThreadNamePrefix

protected String getThreadNamePrefix()
Return the thread name prefix to use for the names of newly created threads.


setThreadPriority

public void setThreadPriority(int threadPriority)
Set the priority of the threads that this executor creates. Default is 5.

See Also:
Thread.NORM_PRIORITY

getThreadPriority

protected int getThreadPriority()
Return the priority of the threads that this executor creates.


setDaemon

public void setDaemon(boolean daemon)
Set whether this executor should create daemon threads, just executing as long as the application itself is running.

Default is "false": Tasks passed to this executor should be either short-lived or support explicit cancelling. Hence, if the application shuts down, tasks will by default finish their execution. Specify "true" for eager shutdown of threads that execute tasks.

See Also:
Thread.setDaemon(boolean)

isDaemon

protected boolean isDaemon()
Return whether this executor should create daemon threads.


execute

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

Specified by:
execute in interface TaskExecutor
Parameters:
task - the Runnable to execute (never null)
Throws:
NullPointerException - if the supplied task is null
See Also:
ConcurrencyThrottleSupport.beforeAccess(), doExecute(Runnable), ConcurrencyThrottleSupport.afterAccess()

doExecute

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

Default implementation creates a new Thread and starts it.

Parameters:
task - the Runnable to execute
See Also:
createThread(java.lang.Runnable), Thread.start()

createThread

protected Thread createThread(Runnable task)
Template method for creation of a Thread.

Default implementation creates a new Thread for the given Runnable, applying an appropriate thread name.

Parameters:
task - the Runnable to execute
See Also:
nextThreadName()

nextThreadName

protected String nextThreadName()
Return the thread name to use for a newly created thread.

Default implementation returns the specified thread name prefix with an increasing thread count appended: for example, "SimpleAsyncTaskExecutor-0".

See Also:
getThreadNamePrefix()

The Spring Framework

Copyright © 2002-2006 The Spring Framework.