java - 具有最小和最大线程数的 ScheduledExecutorService
问题描述
我正在尝试使用 ScheduledExecutorService 在给定的线程池上安排任务。
public ScheduledExecutorService scheduledExecutorService() {
return Executors.newScheduledThreadPool(3);
}
看起来这种创建实例的方式假定用户指定的值作为最小线程数,最大线程数将是Integer.MAX_VALUE
(默认值)。
如何为 ScheduledExecutorService 的实例指定最大线程数?我无法将如此庞大的线程数分配为最大线程数。
反编译后的 jar 序列中的代码片段 -
// From Executors.java
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
// From ScheduledThreadPoolExecutor.java
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE,
DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
new DelayedWorkQueue());
}
// From ThreadPoolExecutor.java
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
如果我在 Java 文档中缺少引用方法,请指出正确的位置。TIA。
解决方案
Constructor
ofScheduledThreadPoolExecutor
不直接提供该参数的设置。您可以手动调用setMaximumPoolSize
方法:
public ScheduledExecutorService scheduledExecutorService() {
ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(corePoolSize);
scheduledThreadPoolExecutor.setMaximumPoolSize(xxx);
return scheduledThreadPoolExecutor;
}
但是在 中设置 thismaximumPoolSize
是没有意义的ScheduledThreadPoolExecutor
,这也是构造函数没有声明这个参数的原因。
来自 Java 文档ScheduledThreadPoolExecutor
:
虽然这个类继承自 ThreadPoolExecutor,但一些继承的调优方法对它没有用。特别是,因为它充当使用 corePoolSize 线程和无界队列的固定大小的池,所以对 maximumPoolSize 的调整没有有用的效果。此外,将 corePoolSize 设置为零或使用 allowCoreThreadTimeOut 几乎不是一个好主意,因为一旦它们有资格运行,这可能会使池没有线程来处理任务。
更多细节,见和 ,delayedExecute
是ensurePrestart
无用的。ScheduledThreadPoolExecutor
maximumPoolSize
private void delayedExecute(RunnableScheduledFuture<?> task) {
if (isShutdown())
reject(task);
else {
// add task to queue
super.getQueue().add(task);
if (isShutdown() &&
!canRunInCurrentRunState(task.isPeriodic()) &&
remove(task))
task.cancel(false);
else
ensurePrestart();
}
}
void ensurePrestart() {
int wc = workerCountOf(ctl.get());
if (wc < corePoolSize)
addWorker(null, true);
else if (wc == 0)
addWorker(null, false);
}
推荐阅读
- javascript - 如何让 Transcrypt 编译为对象而不是 dict?
- c++ - 可以将逗号和参数传递给 C++ 宏吗?
- javascript - 不允许使用 ESC 键关闭模态窗口
- javascript - scayt 按钮不显示,也没有 scayt 功能工作
- javascript - jquery 数据表返回倒数第二行而不是最后一行
- openstreetmap - Overpass-turbo 以 GeoJSON 形式获得响应
- excel - 从工作表加载图像
- python - 如何在 Google App Engine 中使用任务队列 API?
- java - 将日期转换为分钟的错误是什么?
- node.js - res.redirect 不是 promise 中的函数