我试图一起使用ExecutorCompletionService和ScheduledExecutorService。
我需要做的是安排不同的活动,每个活动在执行前都有延迟"然后"重新安排他们" (根据上次运行的结果,有哪些不同的延迟)。"
我遇到的问题是我无法使用ExcecutorCompletionService提交"延迟"
我尝试了以下内容,但它永远阻止......
显然,我错过了Java语言中的一个基本问题。
无论如何都要将任务安排到ScheduledExecutorService,以便CompletionService"知道它"?
public class Bar {
private ScheduledExecutorService scheduledExecutor;
private Future<Status> action1Future;
private Future<Status> action2Future;
private ExecutorCompletionService<Status> pool;
private long delay1 = 10;
private long delay2 = 20;
private long delay3 = 30;
public void start() {
scheduledExecutor = Executors.newScheduledThreadPool(3);
Action1 a1 = new ActionOne(); // Action1 implements Callable<Status>
Action2 a2 = new ActionTwo(); // Action2 implements Callable<Status>
pool = new ExecutorCompletionService<Status>(scheduledExecutor);
action1Future = scheduledExecutor.schedule(a1, delay1, TimeUnit.SECONDS);
action2Future = scheduledExecutor.schedule(a2, delay1, TimeUnit.SECONDS);
monitorAndRestart();
}
private void monitorAndRestart() {
boolean isDone=false;
do {
try {
// THIS IS WHERE IT BLOCKS.
Future<Status> processedItem = pool.get();
if (processedItem == action1Future) {
if (processedItem.get() == Status.GOOD) {
action1Future = scheduledExecutor.schedule(new ActionOne(), delay1, TimeUnit.SECONDS);
} else {
action1Future = scheduledExecutor.schedule(new ActionOne(), delay2, TimeUnit.SECONDS);
}
} else if (processedItem == action2Future) {
if (processedItem.get() == Status.GOOD) {
action1Future = scheduledExecutor.schedule(new ActionOne(), delay2, TimeUnit.SECONDS);
} else {
action1Future = scheduledExecutor.schedule(new ActionOne(), delay3, TimeUnit.SECONDS);
}
}
} catch (InterruptedException e) {
isDone = true;
// handle this.. shudown whatever
}
catch (ExecutionException e) {
// handle this
}
} while (isDone == false);
}
public static void main(String[] args) {
Bar myRunner = new Bar();
myRunner.start();
}
}
如果我把&#34;延迟放在Callable&#34;通过新的ActionOne创建可调用的(延迟);并使用CompletionService.submit(..)它的工作原理。
actionFuture1 = pool.submit(new ActionOne(delay1));
/////
public class ActionOne implements Callable<Status>(
private final delay;
public ActionOne(long dl) {
delay=dl;
}
Status call() {
try {
Thread.sleep(delay * 1000); // seconds
return doSomething()
} catch (...) { //thread.sleep execptions}
}
}
所以我想最后一个问题如下:通过Thread.sleep(延迟)方式,ScheduledExecutorService是否有一些基本上更好的方法呢?
答案 0 :(得分:1)
我们在其中一个应用程序中进行了这种重新安排,我们决定像Ed Thomas所建议的那样包装任务。 (我们还通过传递一个返回任务的下一个执行时间的迭代器使重新安排超级灵活 - 允许我们使用许多不同的调度策略。)
另一种选择是子类ScheduledThreadPoolExecutor并覆盖afterExecute。这可能最终比包装任务更清晰。
答案 1 :(得分:0)
我不认为CompletionService会起作用,特别是当你试图重新安排它时。你可以使用Guava库并切换到ListenableFutures吗?这样可以让自己更轻松地构建它。
对于不同的方法,请考虑让期货/任务自行重新安排。您甚至可以从原始任务的包装器处理此问题(很难从代码中了解最终目标是什么)。
很难准确说出你需要什么模式,但也许你可以玩这样的东西:
public static void scheduleRepeatedly(final Callable<Status> callable, final int someDelay, final ScheduledExecutorService executor) {
executor.schedule(new Runnable() {
@Override
public void run() {
Status status = Status.BAD;
try {
status = callable.call();
} catch (Exception e) {
// whatever logic here to determine if callable should be rescheduled and what the new delay is
if (status == Status.BAD) {
scheduleRepeatedly(callable, someDelay * 2, executor);
}
}
}
}, someDelay, TimeUnit.SECONDS);
}