Spring集成Quartz---Executor框架

2024-08-23 01:58

本文主要是介绍Spring集成Quartz---Executor框架,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


ThreadPool衍生开来将还有一个特定的Executor框架。下文将详细展开讲。

1.Executor框架简介

1.1 概述
在程序运行过程中,创建和销毁线程需要一定的开销,因此如果我们为每一个任务创建一个新线程来执行,这些线程的创建及销毁将消耗大量的计算机资源。尤其是在线程任务不是特别重的情况下,系统的资源有可能极大的耗费在线程的创建与销毁上。

因此,我们需要一种容器去集中的管理这些线程,使得已有的线程能够得到统一的管理和复用。

Java的线程既是工作单元,又是执行机制。从JDK5开始,把工作单元和执行机制分离开来。工作单元包括Runnable和Callable(即实现了这两个接口的类),而执行机制则由Executor框架提供(提供两种典型的线程池:ThreadPoolExecutor和ScheduleThreadPoolExecutor作为工作单元的执行机制)。
1.2 工作单元
在Executor框架中,工作单元其实就是一个实现了Runnable接口或者Callable接口的普通类,该类必须实现对应的run()方法或者call()方法,而该类的实例化对象即为一个工作单元,可以作为任务提交给线程池,由线程池创建相应的线程去执行这个工作单元。

1.3 执行机制
有了上文对工作单元的阐释,执行机制就相对好理解一些,他的本质就是一个线程池,每当有工作单元提交时,线程池需要及时接收这些工作单元,并在后续步骤中,创建相应的线程去执行这些工作单元。同时线程池还需要对这些线程进行统一的管理:创建和销毁。

1.4 Executor二级调度模型

Executor二级调度模型如下图所示。


如上图所示(上图来源于《Java并发编程的艺术》),两级调度模型中,OSKernel(操作系统内核)以上为上级,以下为下级。

在上级中,一个个任务其实就代表着工作单元,这些工作单元通过Executor交由执行机制进行处理。通常Java多线程程序会把应用分解为若干个任务,然后使用用户级的调度器(Executor框架)将这些任务映射为固定数量的线程;
在下级中,操作系统内核将上层线程映射到硬件处理器上。
从上图可以看出,应用程序通过Executor框架控制上层的调度;而下层的调度由操作系统内核控制,下层的调度不受应用程序的控制。他只需要完成对线程的一一映射即可。

2.Executor类成员详解

2.1 成员分类

在Executor框架中,主要的成员可以分为三类。
第一类,即任务,任务是工作的基本单元,离开任务,整个框架就失去了存在的意义。包括被执行的任务需要实现的Runnable或者Callable接口。

第二类,任务调度,有了任务还不行,必须要有相应的执行机制去执行这些任务。包括任务执行的核心接口Executor接口,以及继承自Executor接口的ExecutorService接口。Executor框架有两个关键的实现类实现了ExecutorService接口(ThreadPoolExecutor和ScheduleThreadPoolExecutor

第三类,异步计算的结果,任务交由任务调度执行之后,返回的结果称为异步计算的结果。这里涉及到的同步异步的概念,可以参考相应的百度百科,因为异步不会导致线程阻塞,较为高效,因此咋Executor框架中,采用异步计算的结果。包括接口Future和实现了Future接口的FutureTask类及ScheduleFutureTask(配合ScheduleThreadPoolExecutor使用)。

整个Executor框架的类成员图如下所示。


由上图,可知。
左侧以FutureTask为主线的类是异步计算结。右侧的类则为线程池。

3.ThreadPoolExecutor线程池详解 

可参考Java并发编程:线程池的使用


这里补充一些ThreadPoolExecutor线程池的使用场景。
Executor可以创建三种不同类型的ThreadPoolExecutor,都是通过ExecutorService提供的静态方法进行创建和初始化:
1. FixedThreadPoolExecutor。表示创建固定数量的线程的线程池,即创建一个可复用固定数量线程的线程池,同时采用无界队列去存放提交的任务。源码如下:
 /*** Creates a thread pool that reuses a fixed number of threads* operating off a shared unbounded queue, using the provided* ThreadFactory to create new threads when needed.  At any point,* at most {@code nThreads} threads will be active processing* tasks.  If additional tasks are submitted when all threads are* active, they will wait in the queue until a thread is* available.  If any thread terminates due to a failure during* execution prior to shutdown, a new one will take its place if* needed to execute subsequent tasks.  The threads in the pool will* exist until it is explicitly {@link ExecutorService#shutdown* shutdown}.** @param nThreads the number of threads in the pool* @param threadFactory the factory to use when creating new threads* @return the newly created thread pool* @throws NullPointerException if threadFactory is null* @throws IllegalArgumentException if {@code nThreads <= 0}*/public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(),threadFactory);}
这里注意参数 nThreads,表示初始化过程中指定的CorePoolSize。该线程池适用于为了满足资源管理需求,而需要现在当前线程数量的应用场景,适用于负载比较重的服务器。

2. SingleThreadPoolExecutor。表示创建一个使用单一工作线程的线程池,适用于需要保证顺序的执行各个任务;并且在任意时间点,不会有多个线程是活动的应用场景。源码如下:
/*** Creates an Executor that uses a single worker thread operating* off an unbounded queue. (Note however that if this single* thread terminates due to a failure during execution prior to* shutdown, a new one will take its place if needed to execute* subsequent tasks.)  Tasks are guaranteed to execute* sequentially, and no more than one task will be active at any* given time. Unlike the otherwise equivalent* {@code newFixedThreadPool(1)} the returned executor is* guaranteed not to be reconfigurable to use additional threads.** @return the newly created single-threaded Executor*/public static ExecutorService newSingleThreadExecutor() {return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>()));}
3. CacheThreadPoolExecutor。该线程池是大小无界的线程池,适用于执行很多短期异步任务的小程序,或者是负载较轻的服务器。源码如下:
/*** Creates a thread pool that creates new threads as needed, but* will reuse previously constructed threads when they are* available.  These pools will typically improve the performance* of programs that execute many short-lived asynchronous tasks.* Calls to {@code execute} will reuse previously constructed* threads if available. If no existing thread is available, a new* thread will be created and added to the pool. Threads that have* not been used for sixty seconds are terminated and removed from* the cache. Thus, a pool that remains idle for long enough will* not consume any resources. Note that pools with similar* properties but different details (for example, timeout parameters)* may be created using {@link ThreadPoolExecutor} constructors.** @return the newly created thread pool*/public static ExecutorService newCachedThreadPool() {return new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue<Runnable>());}

4.ScheduleThreadPoolExecutor线程池详解

该部分内容篇幅较长,将独立出一章章节进行阐述。

这篇关于Spring集成Quartz---Executor框架的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1098026

相关文章

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd