源码:java.util.concurrent.ScheduledThreadPoolExecutor 源码解析

1. TODO


2. 脑图

  1. Xmind

  2. Edraw

  3. 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

ScheduledThreadPoolExecutorThreadPoolExecutor 的基础上,只做了三件事:

  1. 使用 ScheduledThreadPoolExecutor.DelayedWorkQueue 作为任务队列
  2. 使用 ScheduledThreadPoolExecutor.ScheduledFutureTask 而不是 FutureTask

[!NOTE] 注意事项

  1. 详见源码:ThreadPoolExecutor
    1. obsidian 内部链接:
      1. 源码:java.util.concurrent.ThreadPoolExecutor源码解析
    2. Hexo 链接:
      1. http://blog.wangjia.ink/2025/09/03/源码:java.util.concurrent.ThreadPoolExecutor源码解析/
  2. 详见源码:ScheduledExecutorService
    1. obsidian 内部链接:
      1. 源码:java.util.concurrent.ScheduledExecutorService源码解析
    2. Hexo 链接:
      1. http://blog.wangjia.ink/2025/11/03/源码:java.util.concurrent.ScheduledExecutorService源码解析/

4. 内部类

4.1. DelayedWorkQueue

详见笔记:Java 数据类型

  1. obsidian 内部链接
    1. 笔记:Java数据类型
  2. Hexo 链接

4.2. ScheduledFutureTask

详见源码:ScheduledFutureTask<V>

  1. obsidian 内部链接:
    1. 源码:java.util.concurrent.ScheduledThreadPoolExecutor.ScheduledFutureTask<V>源码解析
  2. Hexo 链接:
    1. 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] 注意事项

  1. DEFAULT_KEEPALIVE_MILLISScheduledThreadPoolExecutor::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;
}


源码:java.util.concurrent.ScheduledThreadPoolExecutor 源码解析
https://wangjia5289.github.io/2025/11/04/源码:java.util.concurrent.ScheduledThreadPoolExecutor源码解析/
Author
咸阳猴🐒
Posted on
November 4, 2025
Licensed under