Last updated on 2025-11-23T21:19:25+08:00
1. TODO
2. 脑图
Xmind
Edraw
Hexo 地址
👉 http://blog.wangjia.ink/2025/11/04/源码:java.util.concurrent.ScheduledThreadPoolExecutor源码解析/
3. 基础部分
3.1. ScheduledThreadPoolExecutor 概述
ScheduledThreadPoolExecutor 是一个具体类,继承了 java.util.concurrent.ThreadPoolExecutor,实现了 java.util.concurrent.ScheduledExecutorService
ScheduledThreadPoolExecutor 在 ThreadPoolExecutor 的基础上,只做了三件事:
- 使用
ScheduledThreadPoolExecutor.DelayedWorkQueue 作为任务队列
- 使用
ScheduledThreadPoolExecutor.ScheduledFutureTask 而不是 FutureTask
[!NOTE] 注意事项
- 详见源码:
ThreadPoolExecutor
obsidian 内部链接:
- 源码:java.util.concurrent.ThreadPoolExecutor源码解析
Hexo 链接:
- http://blog.wangjia.ink/2025/09/03/源码:java.util.concurrent.ThreadPoolExecutor源码解析/
- 详见源码:
ScheduledExecutorService
obsidian 内部链接:
- 源码:java.util.concurrent.ScheduledExecutorService源码解析
Hexo 链接:
- http://blog.wangjia.ink/2025/11/03/源码:java.util.concurrent.ScheduledExecutorService源码解析/
4. 内部类
4.1. DelayedWorkQueue
详见笔记:Java 数据类型
obsidian 内部链接
- 笔记:Java数据类型
Hexo 链接
4.2. ScheduledFutureTask
详见源码:ScheduledFutureTask<V>
obsidian 内部链接:
- 源码:java.util.concurrent.ScheduledThreadPoolExecutor.ScheduledFutureTask<V>源码解析
Hexo 链接:
- http://blog.wangjia.ink/2025/11/15/源码:java.util.concurrent.ScheduledThreadPoolExecutor.ScheduledFutureTask<V>源码解析/
5. 构造方法
5.1. ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler)
1 2 3 4 5 6 7
| public ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler) { super(corePoolSize, Integer.MAX_VALUE, DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS, new DelayedWorkQueue(), threadFactory, handler); }
|
[!NOTE] 注意事项
DEFAULT_KEEPALIVE_MILLIS 是 ScheduledThreadPoolExecutor::DEFAULT_KEEPALIVE_MILLIS ,值为:10L
6. 实例方法
6.1. 实例具体方法
6.1.1. 具体方法(实现)
6.1.1.1. Executor 中接口方法的实现
6.1.1.1.1. void execute(Runnable command)
1 2 3 4 5
| public void execute(Runnable command) { schedule(command, 0, NANOSECONDS); }
|
6.1.1.2. ExecutorService 中接口方法的实现
6.1.1.2.1. Future<?> submit(Runnable task)
1 2 3 4 5
| public Future<?> submit(Runnable task) { return schedule(task, 0, NANOSECONDS); }
|
6.1.1.2.2. <T> Future<T> submit(Runnable task, T result)
1 2 3 4 5
| public <T> Future<T> submit(Runnable task, T result) { return schedule(Executors.callable(task, result), 0, NANOSECONDS); }
|
6.1.1.2.3. <T> Future<T> submit(Callable<T> task)
1 2 3 4 5
| public <T> Future<T> submit(Callable<T> task) { return schedule(task, 0, NANOSECONDS); }
|
6.1.1.3. ScheduledExecutorService 中接口方法的实现
6.1.1.3.1. ScheduledFuture<?> schedule(Runnable comm, long delay, TimeUnit unit)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { if (command == null || unit == null) throw new NullPointerException(); RunnableScheduledFuture<Void> t = decorateTask(command, new ScheduledFutureTask<Void>(command, null, triggerTime(delay, unit), sequencer.getAndIncrement())); delayedExecute(t); return t; }
|
6.1.1.3.2. <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
1 2 3 4 5 6 7 8 9 10 11 12
| public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { if (callable == null || unit == null) throw new NullPointerException(); RunnableScheduledFuture<V> t = decorateTask(callable, new ScheduledFutureTask<V>(callable, triggerTime(delay, unit), sequencer.getAndIncrement())); delayedExecute(t); return t; }
|
6.1.1.3.3. ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { if (command == null || unit == null) throw new NullPointerException(); if (period <= 0L) throw new IllegalArgumentException(); ScheduledFutureTask<Void> sft = new ScheduledFutureTask<Void>(command, null, triggerTime(initialDelay, unit), unit.toNanos(period), sequencer.getAndIncrement()); RunnableScheduledFuture<Void> t = decorateTask(command, sft); sft.outerTask = t; delayedExecute(t); return t; }
|
6.1.1.3.4. ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { if (command == null || unit == null) throw new NullPointerException(); if (delay <= 0L) throw new IllegalArgumentException(); ScheduledFutureTask<Void> sft = new ScheduledFutureTask<Void>(command, null, triggerTime(initialDelay, unit), -unit.toNanos(delay), sequencer.getAndIncrement()); RunnableScheduledFuture<Void> t = decorateTask(command, sft); sft.outerTask = t; delayedExecute(t); return t; }
|