gpt4 book ai didi

java - Quartz 调度器 theadpool

转载 作者:搜寻专家 更新时间:2023-10-31 20:07:15 25 4
gpt4 key购买 nike

随 Quartz Scheduler 一起提供的 SimpleThreadPool 类没有 FIFO 行为。我想确保如果我继续向调度程序添加作业,它们将以先进先出的方式处理。是否有可用的线程池?或者还有其他方法可以实现吗?

最佳答案

您可以通过委托(delegate)给具有 FIFO 队列的 ThreadPoolExecutor 来实现此目的,如下所示:

public class DelegatingThreadPool implements ThreadPool {

private int size = 5; //Fix this up if you like
private final ThreadPoolExecutor executor = new ThreadPoolExecutor(size, size,
0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());

public boolean runInThread(Runnable runnable) {
synchronized (executor) {
if (executor.getActiveCount() == size) {
return false;
}
executor.submit(runnable);
return true;
}
}

public int blockForAvailableThreads() {
synchronized (executor) {
return executor.getActiveCount();
}
}

public void initialize() throws SchedulerConfigException {
//noop
}

public void shutdown(boolean waitForJobsToComplete) {
//No impl provided for wait, write one if you like
executor.shutdownNow();
}

public int getPoolSize() {
return size;
}

public void setInstanceId(String schedInstId) {
//Do what you like here
}

public void setInstanceName(String schedName) {
//Do what you like here
}

可执行文件的 Activity 计数可能与正在执行的任务的确切数量不完全匹配。您需要添加一个闩锁并使用 beforeExecute 来确保如果有必要,任务已经开始运行。

关于java - Quartz 调度器 theadpool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2994299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com