Spring集成Quartz--ScheduleThreadPool详解

2024-08-23 01:58

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

1.简介

关于ScheduledThreadPoolExecutor大致特性,JDK1.8中是这么介绍的。

A ThreadPoolExecutor that can additionally schedule
commands to run after a given delay, or to execute
periodically. This class is preferable to {@link java.util.Timer}
when multiple worker threads are needed, or when the additional
flexibility or capabilities of {@link ThreadPoolExecutor} (which
this class extends) are required.

首先,ScheduledThreadPoolExecutor的本质还是一个线程池,只是在线程池的基础之上,对用户提交的任务做了更加灵活的处理:
  a)能够在指定时间之后执行用户提交的任务(定时处理)
  b)能够周期性的执行用户提交的任务
  c)当在多线程环境下时,ScheduledThreadPoolExecutor优于Timer。
  
而熟悉线程池的人都知道,线程池内部需要一个任务阻塞队列来协助线程池中任务的有序运行。而在ScheduledThreadPoolExecutor中,应用了DelayWorkQueue延时队列,来实现对任务的定时执行,并进行排序。JDK1.8 中介绍如下。

Using a custom queue (DelayedWorkQueue), a variant of unbounded DelayQueue. The lack of capacity constraint and the fact that corePoolSize and maximumPoolSize are effectively identical simplifies some execution mechanics (see delayedExecute) compared to ThreadPoolExecutor.

首先,ScheduleThreadPool中使用了自定义的队列——DelayWorkQueue,一个无界的延时队列来维护用户或者应用系统提交的任务。相比于ThreadPoolExecutor,ScheduleThreadPool采用了无界队列,对队列容量的大小失去了约束,使得参数corePoolSize 和maximumPoolSize 在数值上保持一致(即使设置了maxmumPoolSize也没有意义。)

2 ScheduleThreadPool类继承结构
类结构
从上图可以看出 ,ScheduleThreadPool 继承自ThreadPoolExecutor,并实现了ScheduledExecutorService接口。
ThreadPoolExecutor的相关介绍请参考ThreadPoolExecutor线程池详解
而其ScheduledExecutorService接口声明如下:

public interface ScheduledExecutorService extends ExecutorService {public ScheduledFuture<?> schedule(Runnable command,long delay, TimeUnit unit);public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit);public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit);public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,long initialDelay,long delay,TimeUnit unit);}

ScheduledExecutorService中声明了四个方法,主要是为了实现对提交的任务进行周期性的执行功能。
  1. schedule(Runnable command,long delay, TimeUnit unit); 若调用此接口,提交的任务将从现在起,经过delay时间段(unit为时间单位)后执行。
  2. schedule(Callable callable,long delay, TimeUnit unit);其功能与1一样,只是参数有所不同,允许传入callable类型参数,并返回计算结果供后续使用。
  3. scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit);表示传入的任务,将按照固定的时间周期执行。
  4. scheduleWithFixedDelay(Runnable command,long initialDelay,long delay,TimeUnit unit);类似于3,此处不再介绍。
3. ScheduleThreadPool关键特性
  3.1 任务阻塞队列–DelayQueue
  
  
  3.2 周期任务–ScheduleFutureTask
  
  3.3 ScheduleThreadPool核心方法–submit()与execute()
4 ScheduleThreadPool其他特性
5 ScheduleThreadPool实例

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



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

相关文章

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

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

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

破茧 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

Python标准库之数据压缩和存档的应用详解

《Python标准库之数据压缩和存档的应用详解》在数据处理与存储领域,压缩和存档是提升效率的关键技术,Python标准库提供了一套完整的工具链,下面小编就来和大家简单介绍一下吧... 目录一、核心模块架构与设计哲学二、关键模块深度解析1.tarfile:专业级归档工具2.zipfile:跨平台归档首选3.

Spring WebClient从入门到精通

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

idea的终端(Terminal)cmd的命令换成linux的命令详解

《idea的终端(Terminal)cmd的命令换成linux的命令详解》本文介绍IDEA配置Git的步骤:安装Git、修改终端设置并重启IDEA,强调顺序,作为个人经验分享,希望提供参考并支持脚本之... 目录一编程、设置前二、前置条件三、android设置四、设置后总结一、php设置前二、前置条件