gpt4 book ai didi

java - 有没有办法将 ScheduledExecutorService 与 ExecutorCompletionService 一起使用?

转载 作者:搜寻专家 更新时间:2023-11-01 03:38:07 25 4
gpt4 key购买 nike

我正在尝试同时使用 ExecutorCompletionService 和 ScheduledExecutorService。

我需要做的是安排不同的 Activity ,每个 Activity 都有“执行前的延迟”,然后根据上次运行的结果“重新安排它们”(不同的延迟)。”

我遇到的问题是我不能使用带有“延迟”的 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();
}
}

如果我把“delay in the Callable”通过 new ActionOne(delay) 创建可调用对象;并使用 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}
}
}

所以我想最后一个问题如下:ScheduledExecutorService 是否有比 Thread.sleep(delay) 方法更根本的东西?

最佳答案

我们在我们的一个应用程序中进行了这种重新安排,我们决定像 Ed Thomas 建议的那样包装任务。 (我们还通过传递一个返回任务下一次执行时间的迭代器,使重新调度变得非常灵活 - 允许我们使用许多不同的调度策略)。

另一种选择是继承 ScheduledThreadPoolExecutor 并覆盖 afterExecute。这最终可能比包装任务更干净。

关于java - 有没有办法将 ScheduledExecutorService 与 ExecutorCompletionService 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23796960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com