Sha*_*mik 5 java quartz-scheduler
随Quartz Scheduler一起提供的SimpleThreadPool类没有FIFO行为.我想确保如果我继续向调度程序添加作业,它们将以先进先出的方式解决.有没有可用的ThreadPool?或者有没有其他方法来实现这一目标?
您可以通过委托具有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
}
Run Code Online (Sandbox Code Playgroud)
可执行文件的活动计数可能与正在执行的任务的确切数量不完全匹配.您需要添加一个锁存器并使用beforeExecute来确保任务已经开始运行(如果有必要).
| 归档时间: |
|
| 查看次数: |
1247 次 |
| 最近记录: |