代码之家  ›  专栏  ›  技术社区  ›  Shamik

石英调度工具

  •  5
  • Shamik  · 技术社区  · 15 年前

    Quartz调度程序附带的SimpleThreadPool类没有FIFO行为。我想确保如果我不断地向调度程序添加作业,它们将以先进先出的方式进行处理。这个有螺纹工具吗?或者有其他方法来实现这个目标吗?

    1 回复  |  直到 15 年前
        1
  •  5
  •   Andrew L    15 年前

    您可以通过向具有FIFO队列的ThreadPool执行器委托来实现这一点,如下所示:

    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来确保 如果有必要,任务已开始运行。