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

相关文章

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Debezium 与 Apache Kafka 的集成方式步骤详解

《Debezium与ApacheKafka的集成方式步骤详解》本文详细介绍了如何将Debezium与ApacheKafka集成,包括集成概述、步骤、注意事项等,通过KafkaConnect,D... 目录一、集成概述二、集成步骤1. 准备 Kafka 环境2. 配置 Kafka Connect3. 安装 D

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.