SimpleAsyncTaskExecutor

异步执行用户任务的SimpleAsyncTaskExecutor。每次执行客户提交给它的任务时,它会启动新的线程,并允许开发者控制并发线程的上限(concurrencyLimit),从而起到一定的资源节流作用。默认时,concurrencyLimit取值为-1,即不启用资源节流。

  1. <bean id="simpleAsyncTaskExecutor"
  2. class="org.springframework.core.task.SimpleAsyncTaskExecutor">
  3. <property name="daemon" value="true"/>
  4. <property name="concurrencyLimit" value="2"/>
  5. <property name="threadNamePrefix" value="simpleAsyncTaskExecutor"/>
  6. </bean>

 

详细图

 

简单图

主要实现

  1. public class SimpleAsyncTaskExecutor extends CustomizableThreadCreator implements AsyncListenableTaskExecutor, Serializable {
  2. //限流主要实现
  3. private final SimpleAsyncTaskExecutor.ConcurrencyThrottleAdapter concurrencyThrottle = new SimpleAsyncTaskExecutor.ConcurrencyThrottleAdapter();
  4. private ThreadFactory threadFactory;
  5. //设置最大的线程数量
  6. public void setConcurrencyLimit(int concurrencyLimit) {
  7. this.concurrencyThrottle.setConcurrencyLimit(concurrencyLimit);
  8. }
  9. //是否开启了限流 限流数量大于0?
  10. public final boolean isThrottleActive() {
  11. return this.concurrencyThrottle.isThrottleActive();
  12. }
  13. //1.是否开启限流 否则不开启限流处理
  14. //2.执行开始之前检测是否可以满足要求 当前数量++
  15. //3.开启限流将执行的Runable进行封装,执行完成调用final方法 当前数量--
  16. public void execute(Runnable task, long startTimeout) {
  17. Assert.notNull(task, "Runnable must not be null");
  18. if(this.isThrottleActive() && startTimeout > 0L) {
  19. this.concurrencyThrottle.beforeAccess();
  20. this.doExecute(new SimpleAsyncTaskExecutor.ConcurrencyThrottlingRunnable(task));
  21. } else {
  22. this.doExecute(task);
  23. }
  24. }
  25. //异步提交有返回值
  26. public Future<?> submit(Runnable task) {
  27. FutureTask future = new FutureTask(task, (Object)null);
  28. this.execute(future, 9223372036854775807L);
  29. return future;
  30. }
  31.  
  32. public <T> Future<T> submit(Callable<T> task) {
  33. FutureTask future = new FutureTask(task);
  34. this.execute(future, 9223372036854775807L);
  35. return future;
  36. }
  37.  
  38. public ListenableFuture<?> submitListenable(Runnable task) {
  39. ListenableFutureTask future = new ListenableFutureTask(task, (Object)null);
  40. this.execute(future, 9223372036854775807L);
  41. return future;
  42. }
  43.  
  44. public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
  45. ListenableFutureTask future = new ListenableFutureTask(task);
  46. this.execute(future, 9223372036854775807L);
  47. return future;
  48. }
  49. //拥有工厂?没有的话调用父类可以设置各种参数的创建线程
  50. protected void doExecute(Runnable task) {
  51. Thread thread = this.threadFactory != null?this.threadFactory.newThread(task):this.createThread(task);
  52. thread.start();
  53. }
  54. //父类的方法,方便配置线程,方便xml设置线程参数CustomizableThreadCreator
  55. public Thread createThread(Runnable runnable) {
  56. Thread thread = new Thread(getThreadGroup(), runnable, nextThreadName());
  57. thread.setPriority(getThreadPriority());
  58. thread.setDaemon(isDaemon());
  59. return thread;
  60. }
  61. }

  

  1. 支持限流处理
  2. 异步注册线程返回结果

限流处理其实就是在执行任务之前和之后对于当前线程数量进行统计

内部类的实现

  1. //下面只是对于操作进行简单的封装,最真的实现还是抽象的ConcurrencyThrottleSupport
  2. private static class ConcurrencyThrottleAdapter extends ConcurrencyThrottleSupport {
  3. private ConcurrencyThrottleAdapter() {
  4. }
  5.  
  6. protected void beforeAccess() {
  7. super.beforeAccess();
  8. }
  9.  
  10. protected void afterAccess() {
  11. super.afterAccess();
  12. }
  13. }
  1. //这里是对于Runable对象执行在次封装,在执行完毕后处理限流操作
  2. private class ConcurrencyThrottlingRunnable implements Runnable {
  3. private final Runnable target;
  4.  
  5. public ConcurrencyThrottlingRunnable(Runnable target) {
  6. this.target = target;
  7. }
  8.  
  9. public void run() {
  10. try {
  11. this.target.run();
  12. } finally {
  13. SimpleAsyncTaskExecutor.this.concurrencyThrottle.afterAccess();
  14. }
  15.  
  16. }
  17. }

  

简单的通过synchronized和wati and notify达到控制线程数量的效果,从而实现限流的策略。

  1. public abstract class ConcurrencyThrottleSupport implements Serializable {
  2. protected transient Log logger = LogFactory.getLog(this.getClass());
  3. private transient Object monitor = new Object();
  4. private int concurrencyLimit = -1;
  5. private int concurrencyCount = 0;
  6. public void setConcurrencyLimit(int concurrencyLimit) {
  7. this.concurrencyLimit = concurrencyLimit;
  8. }
  9.  
  10. public int getConcurrencyLimit() {
  11. return this.concurrencyLimit;
  12. }
  13.  
  14. public boolean isThrottleActive() {
  15. return this.concurrencyLimit > 0;
  16. }
  17. protected void beforeAccess() {
  18. if(this.concurrencyLimit == 0) {
  19. throw new IllegalStateException("没有设置限制");
  20. } else {
  21. if(this.concurrencyLimit > 0) {
  22. boolean debug = this.logger.isDebugEnabled();
  23. Object var2 = this.monitor;
  24. synchronized(this.monitor) {
  25. boolean interrupted = false;
  26. while(this.concurrencyCount >= this.concurrencyLimit) {
  27. if(interrupted) {
  28. throw new IllegalStateException("等待过程中发生中断异常");
  29. }
  30. try {
  31. this.monitor.wait();
  32. } catch (InterruptedException var6) {
  33. Thread.currentThread().interrupt();
  34. interrupted = true;
  35. }
  36. }
  37. ++this.concurrencyCount;
  38. }
  39. }
  40.  
  41. }
  42. }
  43. protected void afterAccess() {
  44. if(this.concurrencyLimit >= 0) {
  45. Object var1 = this.monitor;
  46. synchronized(this.monitor) {
  47. --this.concurrencyCount;
  48. this.monitor.notify();
  49. }
  50. }
  51.  
  52. }
  53. }

异步监听获取线程的结果,其实这个不算这里面的实现

ListenableFutureTask 其实主要是依靠FutureTask这个JDK的封装,覆盖了原始的run方法,在run中封装可以获取到线程的返回值。 
ListenableFutureTask 在次封装,由于FutureTask执行完成之后会调用done()空方法,ListenableFutureTask覆盖done方法可以获取到执行的结果,然后在调用前期注册的错误处理或者成功处理的方法,即可到达异步处理的效果。

类似于回调的效果

  1. public interface SuccessCallback<T> {
  2.  
  3. /**
  4. * Called when the {@link ListenableFuture} successfully completes.
  5. * @param result the result
  6. */
  7. void onSuccess(T result);
  8. }
  9. public interface FailureCallback {
  10.  
  11. /**
  12. * Called when the {@link ListenableFuture} fails to complete.
  13. * @param ex the exception that triggered the failure
  14. */
  15. void onFailure(Throwable ex);
  16. }
  17.  
  18. public interface ListenableFuture<T> extends Future<T> {
  19. //成功和失败的集合
  20. void addCallback(ListenableFutureCallback<? super T> var1);
  21.  
  22. void addCallback(SuccessCallback<? super T> var1, FailureCallback var2);
  23. }

  

实现类(ListenableFutureTask)可有返回值,可被监听的,注册监听,这里可以注册监听者放在一个单独的类中去处理,很好的分配工作ListenableFutureCallbackRegistry

  1. public class ListenableFutureTask<T> extends FutureTask<T> implements ListenableFuture<T> {
  2. private final ListenableFutureCallbackRegistry<T> callbacks = new ListenableFutureCallbackRegistry();
  3.  
  4. public ListenableFutureTask(Callable<T> callable) {
  5. super(callable);
  6. }
  7.  
  8. public ListenableFutureTask(Runnable runnable, T result) {
  9. super(runnable, result);
  10. }
  11.  
  12. public void addCallback(ListenableFutureCallback<? super T> callback) {
  13. this.callbacks.addCallback(callback);
  14. }
  15.  
  16. public void addCallback(SuccessCallback<? super T> successCallback, FailureCallback failureCallback) {
  17. this.callbacks.addSuccessCallback(successCallback);
  18. this.callbacks.addFailureCallback(failureCallback);
  19. }
  20. //FutureTask执行完成后的回调,调用监听接口的实现类的方法
  21. protected final void done() {
  22. Object cause;
  23. try {
  24. Object ex = this.get();
  25. //回调实现类的方法
  26. this.callbacks.success(ex);
  27. return;
  28. } catch (InterruptedException var3) {
  29. Thread.currentThread().interrupt();
  30. return;
  31. } catch (ExecutionException var4) {
  32. cause = var4.getCause();
  33. if(cause == null) {
  34. cause = var4;
  35. }
  36. } catch (Throwable var5) {
  37. cause = var5;
  38. }
  39.  
  40. this.callbacks.failure((Throwable)cause);
  41. }
  42. }

注册监听,还维护了一个状态量的信息,很标准的写法,维护队列的添加和成功消息和失败消息的处理

  1. public class ListenableFutureCallbackRegistry<T> {
  2.  
  3. private final Queue<SuccessCallback<? super T>> successCallbacks = new LinkedList<SuccessCallback<? super T>>();
  4.  
  5. private final Queue<FailureCallback> failureCallbacks = new LinkedList<FailureCallback>();
  6.  
  7. private State state = State.NEW;
  8.  
  9. private Object result = null;
  10.  
  11. private final Object mutex = new Object();
  12.  
  13. /**
  14. * Add the given callback to this registry.
  15. * @param callback the callback to add
  16. */
  17. public void addCallback(ListenableFutureCallback<? super T> callback) {
  18. Assert.notNull(callback, "'callback' must not be null");
  19. synchronized (this.mutex) {
  20. switch (this.state) {
  21. case NEW:
  22. this.successCallbacks.add(callback);
  23. this.failureCallbacks.add(callback);
  24. break;
  25. case SUCCESS:
  26. callback.onSuccess((T) this.result);
  27. break;
  28. case FAILURE:
  29. callback.onFailure((Throwable) this.result);
  30. break;
  31. }
  32. }
  33. }
  34.  
  35. /**
  36. * Add the given success callback to this registry.
  37. * @param callback the success callback to add
  38. * @since 4.1
  39. */
  40. public void addSuccessCallback(SuccessCallback<? super T> callback) {
  41. Assert.notNull(callback, "'callback' must not be null");
  42. synchronized (this.mutex) {
  43. switch (this.state) {
  44. case NEW:
  45. this.successCallbacks.add(callback);
  46. break;
  47. case SUCCESS:
  48. callback.onSuccess((T) this.result);
  49. break;
  50. }
  51. }
  52. }
  53.  
  54. /**
  55. * Add the given failure callback to this registry.
  56. * @param callback the failure callback to add
  57. * @since 4.1
  58. */
  59. public void addFailureCallback(FailureCallback callback) {
  60. Assert.notNull(callback, "'callback' must not be null");
  61. synchronized (this.mutex) {
  62. switch (this.state) {
  63. case NEW:
  64. this.failureCallbacks.add(callback);
  65. break;
  66. case FAILURE:
  67. callback.onFailure((Throwable) this.result);
  68. break;
  69. }
  70. }
  71. }
  72.  
  73. /**
  74. * Trigger a {@link ListenableFutureCallback#onSuccess(Object)} call on all
  75. * added callbacks with the given result.
  76. * @param result the result to trigger the callbacks with
  77. */
  78. public void success(T result) {
  79. synchronized (this.mutex) {
  80. this.state = State.SUCCESS;
  81. this.result = result;
  82. while (!this.successCallbacks.isEmpty()) {
  83. this.successCallbacks.poll().onSuccess(result);
  84. }
  85. }
  86. }
  87.  
  88. public void failure(Throwable ex) {
  89. synchronized (this.mutex) {
  90. this.state = State.FAILURE;
  91. this.result = ex;
  92. while (!this.failureCallbacks.isEmpty()) {
  93. this.failureCallbacks.poll().onFailure(ex);
  94. }
  95. }
  96. }
  97.  
  98. private enum State {NEW, SUCCESS, FAILURE}
  99.  
  100. }

TaskExecutor---Spring异步线程池的接口类,其实质是java.util.concurrent.Executor

以下是官方已经实现的全部7个TaskExecuter。Spring宣称对于任何场景,这些TaskExecuter完全够用了:

名字 特点
SimpleAsyncTaskExecutor 每次请求新开线程,没有最大线程数设置.不是真的线程池,这个类不重用线程,每次调用都会创建一个新的线程。 --【1】
SyncTaskExecutor 不是异步的线程.同步可以用SyncTaskExecutor,但这个可以说不算一个线程池,因为还在原线程执行。这个类没有实现异步调用,只是一个同步操作。
ConcurrentTaskExecutor Executor的适配类,不推荐使用。如果ThreadPoolTaskExecutor不满足要求时,才用考虑使用这个类。
SimpleThreadPoolTaskExecutor 监听Spring’s lifecycle callbacks,并且可以和Quartz的Component兼容.是Quartz的SimpleThreadPool的类。线程池同时被quartz和非quartz使用,才需要使用此类。
ThreadPoolTaskExecutor 最常用。要求jdk版本大于等于5。可以在程序而不是xml里修改线程池的配置.其实质是对java.util.concurrent.ThreadPoolExecutor的包装。
TimerTaskExecutor  
WorkManagerTaskExecutor  

 

Spring中的同步执行器

  1. SyncTaskExecutor:同步可以用SyncTaskExecutor,但这个可以说不算一个线程池,因为还在原线程执行。这个类没有实现异步调用,只是一个同步操作。
  2. 也可以用ThreadPoolTaskExecutor结合FutureTask做到同步。

SyncTaskExecutor与ThreadPoolTaskExecutor区别

前者是同步执行器,执行任务同步,后者是线程池,执行任务异步。

使用ThreadPoolTaskExecutor

比起从线程池取一个线程再执行, 你仅仅需要把你的Runnable类加入到队列中,然后TaskExecutor用它内置的规则决定何时开始取一个线程并执行该Runnable类

先在xml中添加bean的配置:

  1. <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
  2. <property name="corePoolSize" value="5" />
  3. <property name="maxPoolSize" value="10" />
  4. <property name="queueCapacity" value="25" />
  5. </bean>
  6.  
  7. <bean id="taskExecutorExample" class="TaskExecutorExample">
  8. <constructor-arg ref="taskExecutor" />
  9. </bean>

配置解释 

当一个任务通过execute(Runnable)方法欲添加到线程池时:

  1. 如果此时线程池中的数量小于corePoolSize,即使线程池中的线程都处于空闲状态,也要创建新的线程来处理被添加的任务。
  2. 如果此时线程池中的数量等于 corePoolSize,但是缓冲队列 workQueue未满,那么任务被放入缓冲队列。
  3. 如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量小于maximumPoolSize,建新的线程来处理被添加的任务。
  4. 如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量等于maximumPoolSize,那么通过 handler所指定的策略来处理此任务。也就是:处理任务的优先级为:核心线程corePoolSize、任务队列workQueue、最大线程 maximumPoolSize,如果三者都满了,使用handler处理被拒绝的任务。
  5. 当线程池中的线程数量大于 corePoolSize时,如果某线程空闲时间超过keepAliveTime,线程将被终止。这样,线程池可以动态的调整池中的线程数。

推荐 - 使用ThreadPoolTaskExecutor(注解方式)

首先,为了以注解方式使用异步功能,我们需要在Spring的xml配置中定义相关的bean:

1.必须在*-servlet.xml而不是applicationContext.xml中定义@Async相关配置

引自http://stackoverflow.com/questions/6610563/spring-async-not-working

2 不使用线程池版本

引自http://www.springframework.org/schema/task/spring-task-3.2.xsd

所以,如果我们仅仅添加<task:annotation-driven/>,也可以使用@Async标签。然而,此时使用的是SimpleAsyncTaskExecutor。如“官方文档27章:Task Execution”中所述,SimpleAsyncTaskExecutor不会使用线程池,仅仅是为每一个请求新开一个线程。这样在大并发的业务场景下,发生OutOfMemory是不足为奇的。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--Spring框架的xml标签定义文档, 可访问http://www.springframework.org/schema/task/查看最新task组件的xml标签文档-->
  3. <beans xmlns:task="http://www.springframework.org/schema/task"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/task
  6. http://www.springframework.org/schema/task/spring-task-3.2.xsd">
  7. <!--扫描项目实例化@Component,@Service,@Controller修饰的类-->
  8. <context:component-scan base-package="com.your_app" />
  9.  
  10. <!--create a SimpleAsyncTaskExecutor instance-->
  11. <task:annotation-driven/>
  12. </beans>

3.推荐 - 使用线程池版本

  1. ?xml version="1.0" encoding="UTF-8"?>
  2. <!--Spring框架的xml标签定义文档, 可访问http://www.springframework.org/schema/task/查看最新task组件的xml标签文档-->
  3. <beans xmlns:task="http://www.springframework.org/schema/task"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/task
  6. http://www.springframework.org/schema/task/spring-task-3.2.xsd">
  7.  
  8. <!--扫描项目实例化@Component@Service@Controller修饰的类-->
  9. <context:component-scan base-package="com.your_app" />
  10.  
  11. <!-- 在代码中@Async不加参数就会使用task:annotation-driven标签定义的executor-->
  12. <task:annotation-driven executor="myExecutor"/>
  13. <!-- 在代码中@Async("myExecutor")可以显式指定executor"myExecutor"-->
  14. <task:executor id="myExecutor"  
  15. pool-size="5-25"
  16. queue-capacity="100"
  17. rejection-policy="CALLER_RUNS"/>
  18. </beans>

其中,注意到属性pool-size的值”5-25”是一个范围,这对应的是线程池的min和max容量,它们的意义请参考本文上一节的“配置说明”里的第3、4点。如果只有一个值,如pool-size=n, 意味着minSize==maxSize==n

而关于rejection-policy,官方文档总结如下:

池满时的拒绝策略 效果
AbortPolicy(默认) 抛异常
DiscardPolicy or DiscardOldestPolicy 放弃该线程
CallerRunsPolicy 通知该线程的创建者,让其不要提交新的线程

异步执行任务SimpleAsyncTaskExecutor详解的更多相关文章

  1. 「微信小程序」PHP异步进程async-helper实例详解

    PHP异步进程async-helper实例详解 PHP 的异步进程助手,借助于 AMQP 实现异步执行 PHP 的方法,将一些很耗时.追求高可用.需要重试机制的操作放到异步进程中去执行,将你的 HTT ...

  2. MySQL 执行计划explain详解

    MySQL 执行计划explain详解 2015-08-10 13:56:27 分类: MySQL explain命令是查看查询优化器如何决定执行查询的主要方法.这个功能有局限性,并不总会说出真相,但 ...

  3. SQL Server 执行计划操作符详解(3)——计算标量(Compute Scalar)

    接上文:SQL Server 执行计划操作符详解(2)--串联(Concatenation ) 前言: 前面两篇文章介绍了关于串联(Concatenation)和断言(Assert)操作符,本文介绍第 ...

  4. SQL Server 执行计划操作符详解(2)——串联(Concatenation )

    本文接上文:SQL Server 执行计划操作符详解(1)--断言(Assert) 前言: 根据计划,本文开始讲述另外一个操作符串联(Concatenation),读者可以根据这个词(中英文均可)先幻 ...

  5. 批量执行工具PSSH详解

    批量执行工具PSSH详解 pssh是一个python编写可以在多台服务器上执行命令的工具,同时支持拷贝文件,是同类工具中很出色的,使用必须在各个服务器上配置好密钥认证访问. 安装pssh包 yum 安 ...

  6. 《手把手教你》系列技巧篇(四十)-java+ selenium自动化测试-JavaScript的调用执行-下篇(详解教程)

    1.简介 在实际工作中,我们需要对处理的元素进行高亮显示,或者有时候为了看清楚做跟踪鼠标点击了哪些元素需要标记出来.今天宏哥就在这里把这种测试场景讲解和分享一下. 2.用法 创建一个执行 JS 的对象 ...

  7. C#中异步调用示例与详解

    using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServi ...

  8. MySQL命令行下执行.sql脚本详解

    本文主要介绍一个在MySQL命令行下执行脚本文件的例子,通过这个例子让我们来了解一下在命令行下MySQL是怎样执行脚本的吧.现在我们开始介绍这一过程. 1.首先编写sql脚本,保存为的:book.sq ...

  9. MongoDB执行计划分析详解

    要保证数据库处于高效.稳定的状态,除了良好的硬件基础.高效高可用的数据库架构.贴合业务的数据模型之外,高效的查询语句也是不可少的.那么,如何查看并判断我们的执行计划呢?我们今天就来谈论下MongoDB ...

随机推荐

  1. Qt——styleSheet

    1.两个地方调用 QWidget::setStyleSheet() QApplication::setStyleSheet() 2.基本语法 selector {attribute : value} ...

  2. Prim算法堆优化

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> ...

  3. 不使用IDE,用maven命令打包war项目及出现的问题解决(最全攻略,亲测有效)

    第一次在博客园写博客,写的不好大家见谅啊!! 一.首先,在用maven打包之前,一定确认好你使用的jdk版本和maven的版本,最好就是用jdk1.8和maven3.3以上的,版本太低了容易出现一些意 ...

  4. 成都Uber优步司机奖励政策(1月13日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  5. 在线大数据cdh迁移,从公有云迁移到私有云

    1.添加新节点 2.迁移hdfs和hbase数据: 3.迁移源节点各个服务到新节点: 4.利用HA,包括hdfs,hbase master,resourcemanager的ha实现在线迁移: 5.数据 ...

  6. 跨平台编译Go程序(交叉编译)

    作用:比如你手头只有Mac系统,而你的用户有Linux和Windows的,他们也想用,你可以通过交叉编译出Linux和Windows上的可执行文件给他们用 (1)首先进入go/src 源码所在目录,执 ...

  7. Linux命令应用大词典-第11章 Shell编程

    11.1 declare:显示或设置Shell变量 11.2 export:显示或设置环境变量 11.3 set:显示和设置Shell变量 11.4 unset:删除变量或函数 11.5 env:查看 ...

  8. 【WXS全局对象】Date

    属性: 名称 说明 Date.parse( [dateString] ) 解析一个日期时间字符串,并返回 1970/1/1 午夜距离该日期时间的毫秒数. Date.UTC(year,month,day ...

  9. POJ 3046

    题目大意:蚂蚁牙黑,蚂蚁牙红:有A只蚂蚁,来自T个家族,分别记为ant[i]个.同一个家族的蚂蚁长得一样,但是不同家族的蚂蚁牙齿颜色不同.任取n只蚂蚁(S <= n <= B),求能组成几 ...

  10. DataTable转Json,Json转DataTable

    // 页面加载时 /// </summary> /// <param name="sender"></param> /// <param ...