java多线程学习之定时器线程池的使用ScheduledThreadPoolExecutor

本文解析了ScheduledThreadPoolExecutor,一种专为按时间计划执行任务的线程池,讲解了其构造特性、调度机制以及与java.util.Timer的区别。特别关注了它的核心池大小、任务队列特性和时间调度功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class ScheduledThreadPoolExecutor extends ThreadPoolExecutor {

    public ScheduledThreadPoolExecutor(int corePoolSize);

    public ScheduledThreadPoolExecutor(int corePoolSize,

                              ThreadFactory threadFactory);

    public ScheduledThreadPoolExecutor(int corePoolSize,

                              RejectedExecutionHandler handler);

    public ScheduledThreadPoolExecutor(int corePoolSize,

                              ThreadFactory threadFactory,

                              RejectedExecutionHandler handler);

    public <V> ScheduledFuture<V> schedule(Callable<V> callable,

                    long delay, TimeUnit unit);

    public ScheduledFuture<V> scheduleAtFixedRate(Runnable command,

                    long initialDelay, long period, TimeUnit unit);

    public ScheduledFuture<V> scheduleWithFixedDelay(

                    Runnable command, long initialDelay,

                    long delay, TimeUnit unit);

    public void execute(Runnable command);

    public void shutdown( );

    public List shutdownNow( );

            

    public void setContinueExistingPeriodicTasksAfterShutdownPolicy(

                                   boolean value);

    public boolean getContinueExistingPeriodicTasksAfterShutdownPolicy( );

    public void setExecuteExistingDelayedTasksAfterShutdownPolicy(

                                   boolean value);

    public boolean getExecuteExistingDelayedTasksAfterShutdownPolicy( );

}
_______________________________________________________________________________________________________________________
由上面的代码首先看出ScheduledThreadPoolExecutor 本质上是一个线程池(即thread pool)但是要注意它又是一个具有特殊性质和功能的线程池,首先就线程池的特性来说:
ScheduledThreadPoolExecutor 的构造也是由可存放多个thread的 Thread[] pool(存放多个工作线程用的池子)和一个可排队式存放任务实例用的Queue<Runnable> tasksQueue(任务队列,工作队列)这两种部件所组成的!其内部也存在线程池固有的调度算法,但是要注意一点:该pool池(存放工作线程thread的地方)的容量大小总是被固定死在corePoolSize上面,即仅仅可以设置pool内核心业务处理线程的总数目,而没有线程池中最大可扩展的线程总数目这个概念,另外ScheduledThreadPoolExecutor 的任务队列Queue<Runnable> tasksQueue的大小是不受限制的,即任务队列永远不会拒绝新任务的入队列操作.这都是ScheduledThreadPoolExecutor 自己独有的特性。

第二:从ScheduledThreadPoolExecutor 中ScheduledThread字样来看ScheduledThreadPoolExecutor 具有按“时间计划”执行任务的特性,即ScheduledThreadPoolExecutor 可以按照时间计划(规定)参数,让pool中的工作thread按时间计划参数去处理在任务队列tasksQueue中排队的Runnable实例们。tasksQueue中的任务实例和pool中工作线程之间的交互,以及pool中工作thread的调度都是由ScheduledThreadPoolExecutor 内部自己的算法自己维护的,开发人员无需操心.

______________________________________________________________________________________________________________________________________________

ScheduledThreadPoolExecutor 与java.util.Timer的比较:
ScheduledThreadPoolExecutor 具有线程池 thread pool的概念可以对需要批量处理的多个可排队的可执行任务(Runnable任务)进行FIFO方式的批量处理,并可以避免系统不断创建新thread实例的资源开销.

而java.util.Timer也有它自己独道的优势与特点:第一它提供了指定绝对运行时间点的选项参数(绝对时间点的计划任务的选项),第二:java.util.Timer比较容易使用,比较容易上手,如果你的系统(自开发的软件系统)中仅仅只有几个任务可以并发执行(不需要同时创建大量的Thread类型对象)或者需要重复性的定时执行的任务时,利用java.util.Timer应该是更加合适的选择.


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值