1. spring提供了哪些任务执行器,是否有同步的任务执行器

有ThreadPoolTaskExecutor等执行器

同步可以用SyncTaskExecutor,但这个可以说不算一个线程池,因为还在原线程执行

也可以用ThreadPoolTaskExecutor结合FutureTask做到同步

此外还有

代码:

/*
* 文 件 名: Test000101.java
* 版 权: . Copyright 2008-2015, All rights reserved Information Technology Co.,Ltd.
* 描 述: <描述>
* 修 改 人: chen.simon
* 修改时间: 2016-5-11
*/
package org.simonme.mt.demo; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.task.TaskExecutor; /**
* <一句话功能简述> <功能详细描述>
*
* @author Chenxiaguang
* @version [版本号, 2016-5-11]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class Test000101
{ public static void main(String[] args)
{
execute(provideThreadPoolTaskExecutor());
execute(provideSyncTaskExecutor());
} /** <一句话功能简述>
* <功能详细描述>
* @see [类、类#方法、类#成员]
*/
private static void execute(TaskExecutor taskESExecutor)
{
System.out.println(taskESExecutor);
Callable<String> run = new Callable<String>()
{
public String call()
{
return runTask();
}
}; FutureTask<String> task = new FutureTask<String>(run);
taskESExecutor.execute(task);
System.out.println("Before get"); /**
* 下面的get 利用了FutureTask的阻塞特性,使得主线程等待 TaskExecutor异步执行完后拿到结果<br/>
* 此处虽说用的是异步的TaskExecutor,但是拿结果的动作还是同步的<br/>
*/
try
{
task.get();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (ExecutionException e)
{
e.printStackTrace();
}
System.out.println("After get");
} public static TaskExecutor provideThreadPoolTaskExecutor()
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-0001.xml");
TaskExecutor taskESExecutor000101 = (TaskExecutor)applicationContext.getBean("taskESExecutor000101");
return taskESExecutor000101;
} public static TaskExecutor provideSyncTaskExecutor()
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-0001.xml");
TaskExecutor taskESExecutor000102 = (TaskExecutor)applicationContext.getBean("taskESExecutor000102");
return taskESExecutor000102;
} private static String runTask()
{
try
{
Thread.sleep(2 * 1000l);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
String result = "Finish";
System.out.println(result);
return result;
} }
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-autowire="byName" default-lazy-init="false"> <bean id="taskESExecutor000101" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="10" />
<property name="maxPoolSize" value="50" />
<property name="queueCapacity" value="500" />
<property name="keepAliveSeconds" value="300" />
<property name="rejectedExecutionHandler">
<bean class="java.util.concurrent.ThreadPoolExecutor$DiscardOldestPolicy" />
</property>
</bean> <bean id="taskESExecutor000102" class="org.springframework.core.task.SyncTaskExecutor">
</bean> </beans>

2. SyncTaskExecutor与ThreadPoolTaskExecutor区别

前者是同步执行器,执行任务同步,见下图,还在主线程上

后者是线程池,执行任务异步,见下图,在另外的线程上

ThreadPoolTaskExecutor执行的堆栈图:

SyncTaskExecutor执行的堆栈图:

此外线程池这种任务执行器在任务执行完主线程不会退出。

Spring task executor同异步的更多相关文章

  1. Spring的任务调度@Scheduled注解——task:scheduler和task:executor的解析

    原文地址: https://blog.csdn.net/yx0628/article/details/80873774 一个简单的Spring定时任务的 demo,全部代码见下载地址:https:// ...

  2. Spring源码情操陶冶#task:executor解析器

    承接Spring源码情操陶冶-自定义节点的解析.线程池是jdk的一个很重要的概念,在很多的场景都会应用到,多用于处理多任务的并发处理,此处借由spring整合jdk的cocurrent包的方式来进行深 ...

  3. Spring 使用介绍(十二)—— Spring Task

    一.概述 1.jdk的线程池和任务调用器分别由ExecutorService.ScheduledExecutorService定义,继承关系如下: ThreadPoolExecutor:Executo ...

  4. 任务调度 Spring Task 4(一)

    深入浅出spring task定时任务 在工作中有用到spring task作为定时任务的处理,spring通过接口TaskExecutor和TaskScheduler这两个接口的方式为异步定时任务提 ...

  5. spring task 配置

    Spring对Quartz作了一个封装,同时,Spring自己也提供了一个任务定时器(spring-task),现把它总结一下.    对于Quartz,我们使用的时候主要是注重两个方面,一个是定时任 ...

  6. Quartz Spring与Spring Task总结

    Spring对Quartz作了一个封装,同时,Spring自己也提供了一个任务定时器(spring-task),现把它总结一下.    对于Quartz,我们使用的时候主要是注重两个方面,一个是定时任 ...

  7. spring定时任务轮询(spring Task)

    定时任务轮询比如任务自服务器启动就开始运行,并且每隔5秒执行一次. 以下用spring注解配置定时任务.1.添加相应的schema xmlns:task=" xsi:schemaLocati ...

  8. uartz Spring与Spring Task总结

    Spring对Quartz作了一个封装,同时,Spring自己也提供了一个任务定时器(spring-task),现把它总结一下.    对于Quartz,我们使用的时候主要是注重两个方面,一个是定时任 ...

  9. SpringBoot整合全局异常处理&SpringBoot整合定时任务Task&SpringBoot整合异步任务

    ============整合全局异常=========== 1.整合web访问的全局异常 如果不做全局异常处理直接访问如果报错,页面会报错500错误,对于界面的显示非常不友好,因此需要做处理. 全局异 ...

随机推荐

  1. 【BZOJ】2879: [Noi2012]美食节

    题意 \(m\)个厨师,\(n\)种菜,每种菜需要做\(p_i\)份,每个厨师做第\(i\)种菜用时\(t_{i, j}\).一个厨师做完一道菜才能做下一道.每份菜的时间是这个厨师做完这道菜的用时加上 ...

  2. 清除BOM头源码

    BOM: Byte Order Mark UTF-8 BOM又叫UTF-8 签名,其实UTF-8 的BOM对UFT-8没有作用,是为了支援UTF-16,UTF-32才加上的BOM,BOM签名的意思就是 ...

  3. 1069. The Black Hole of Numbers (20)

    For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...

  4. spring mvc 拦截器 拦截子目录

    项目中碰到这一个问题: 对于/user/loginpage,/user/login这一类的url,放行: 对于/user/{userId}/xxx(xxx不为空)的操作,需要拦截,url-patter ...

  5. 【android应用市场】android应用市场集合

    一.举例 1.酷市场 2.apkpure 国外的应用市场,可不用FQ,没有google play的一些限制 相当于google play的镜像,可以下载google play的应用 3.360手机助手 ...

  6. [LintCode] Segment Tree Build 建立线段树

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  7. cat命令在文件中插入内容

    eg: cat>> xxx <<EOFinsert 1insert 2 EOF

  8. centos7 服务管理

    服务脚本位置: /usr/lib/systemd/system  (开机不登录就能够运行的服务) /usr/lib/systemd/user      (用户登录后才能运行的服务) 服务脚本示例: [ ...

  9. Git基本命令行操作

    A. 新建Git仓库,创建新文件夹git init  B. 添加文件到git索引git add <filename>  --- 单个文件添加git add * --- 全部文件添加 C. ...

  10. 求第N个素数

    埃拉托斯特尼筛法 如果求第n 个素数,有一个数学公式可以得到第n 个素数的上界:uper=n*ln(n)+n*ln(ln(n)),n>=6.如果一个数是素数那么这个数的倍数是非素数因此例如2是素 ...