石英调度器线程池

5

Quartz调度程序附带的SimpleThreadPool类没有FIFO行为。我想确保如果我持续向调度程序添加作业,它们将按照先进先出的原则进行处理。是否有可用的线程池可以实现这一点?或者是否有其他方法可以实现这一点?

1个回答

5
您可以通过将任务委派给具有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
}

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


这是一个很好的例子,我会尝试一下。 - Shamik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接