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

相关文章

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

java.sql.SQLTransientConnectionException连接超时异常原因及解决方案

《java.sql.SQLTransientConnectionException连接超时异常原因及解决方案》:本文主要介绍java.sql.SQLTransientConnectionExcep... 目录一、引言二、异常信息分析三、可能的原因3.1 连接池配置不合理3.2 数据库负载过高3.3 连接泄漏