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中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

Java中Integer128陷阱

《Java中Integer128陷阱》本文主要介绍了Java中Integer与int的区别及装箱拆箱机制,重点指出-128至127范围内的Integer值会复用缓存对象,导致==比较结果为true,下... 目录一、Integer和int的联系1.1 Integer和int的区别1.2 Integer和in

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

JSONArray在Java中的应用操作实例

《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA

Java JDK1.8 安装和环境配置教程详解

《JavaJDK1.8安装和环境配置教程详解》文章简要介绍了JDK1.8的安装流程,包括官网下载对应系统版本、安装时选择非系统盘路径、配置JAVA_HOME、CLASSPATH和Path环境变量,... 目录1.下载JDK2.安装JDK3.配置环境变量4.检验JDK官网下载地址:Java Downloads

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap