Spring task executor同异步
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同异步的更多相关文章
- Spring的任务调度@Scheduled注解——task:scheduler和task:executor的解析
原文地址: https://blog.csdn.net/yx0628/article/details/80873774 一个简单的Spring定时任务的 demo,全部代码见下载地址:https:// ...
- Spring源码情操陶冶#task:executor解析器
承接Spring源码情操陶冶-自定义节点的解析.线程池是jdk的一个很重要的概念,在很多的场景都会应用到,多用于处理多任务的并发处理,此处借由spring整合jdk的cocurrent包的方式来进行深 ...
- Spring 使用介绍(十二)—— Spring Task
一.概述 1.jdk的线程池和任务调用器分别由ExecutorService.ScheduledExecutorService定义,继承关系如下: ThreadPoolExecutor:Executo ...
- 任务调度 Spring Task 4(一)
深入浅出spring task定时任务 在工作中有用到spring task作为定时任务的处理,spring通过接口TaskExecutor和TaskScheduler这两个接口的方式为异步定时任务提 ...
- spring task 配置
Spring对Quartz作了一个封装,同时,Spring自己也提供了一个任务定时器(spring-task),现把它总结一下. 对于Quartz,我们使用的时候主要是注重两个方面,一个是定时任 ...
- Quartz Spring与Spring Task总结
Spring对Quartz作了一个封装,同时,Spring自己也提供了一个任务定时器(spring-task),现把它总结一下. 对于Quartz,我们使用的时候主要是注重两个方面,一个是定时任 ...
- spring定时任务轮询(spring Task)
定时任务轮询比如任务自服务器启动就开始运行,并且每隔5秒执行一次. 以下用spring注解配置定时任务.1.添加相应的schema xmlns:task=" xsi:schemaLocati ...
- uartz Spring与Spring Task总结
Spring对Quartz作了一个封装,同时,Spring自己也提供了一个任务定时器(spring-task),现把它总结一下. 对于Quartz,我们使用的时候主要是注重两个方面,一个是定时任 ...
- SpringBoot整合全局异常处理&SpringBoot整合定时任务Task&SpringBoot整合异步任务
============整合全局异常=========== 1.整合web访问的全局异常 如果不做全局异常处理直接访问如果报错,页面会报错500错误,对于界面的显示非常不友好,因此需要做处理. 全局异 ...
随机推荐
- 定期来一次“绩效谈话”(摘自易中)
值得借鉴学习 管理者和下属直接的互动关系当中,需要有一个定期的反馈机制.员工在工作当中需要管理者给他持续地反馈.以下是一个绩效谈话的标准程序: 一:说明会谈的目的和时间:我们用10分钟对你上一阶段的工 ...
- Ubuntu root密码修改
安装完Ubuntu后忽然意识到没有设置root密码,不知道密码自然就无法进入根用户下.到网上搜了一下,原来是这麽回事.Ubuntu的默认root密码是随机的,即每次开机都有一个新的root密码.我们可 ...
- MongoDB数据库的简介及安装
一.MongoDB数据库简介 简介 MongoDB是一个高性能,开源,无模式的,基于分布式文件存储的文档型数据库,由C++语言编写,其名称来源取自“humongous”,是一种开源的文档数据库──No ...
- Centos 安装 NodeJS
准备命令: yum -y install gcc make gcc-c++ openssl-devel wget 下载源码及解压: wget http://nodejs.org/dist/v0.10. ...
- 如何用bat批处理编译swf项目
平时用FB等IDE编译多模块的游戏项目时,除了添加移除模块的操作很繁琐外,编译速度也非常之慢.而用bat来编译swf项目,速度非常快,稳定. 在此分享自己工作用的bat,每次运行会重新编译主模块Gam ...
- 高性能MySQL第1章知识点梳理
1. MySQL的逻辑架构 最上面不是MySQL特有的,所有基于网络的C/S的网络应用程序都应该包括连接处理.认证.安全管理等. 中间层是MySQL的核心,包括查询解析.分析.优化和缓存等.同时它还提 ...
- view保存为图片
一.概述 简书.微博.便签等都有将文章保存为图片的功能.笔者臆测,此功能的实现原理如下. 二.实现 2.1将View保存成Bitmap对象 方法1(亲测有效) private Bitmap makin ...
- 在html中如何获取表单提交的数据
a.html: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www ...
- Oracle临时表
1概念理解 ORACLE系统的临时表常被用于存放系统操作的中间数据,由于对临时的任何操作都不产生redo(但会因为修改undo而产生redo),所以临时表的数据操作效率一般都比较高.常用的临时表主要有 ...
- IOS第八天(1:UITableViewController团购,数据转模型,xib显示数据)
******HMTg.h 模型数据 #import <Foundation/Foundation.h> @interface HMTg : NSObject @property (nona ...