石英调度程序theadpool
问题描述:
与Quartz Scheduler一起提供的SimpleThreadPool类不具有FIFO行为。我想确保我是否继续向调度程序添加作业,它们是以先进先出的方式处理的。有没有任何ThreadPool可用于此?或者还有其他方法可以实现吗?石英调度程序theadpool
答
你可以通过委托给一个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 2010-06-08 17:24:01