我正在使用Spring Task Scheduler来安排定期任务。
ScheduledFuture scheduleAtFixedRate(Runnable task, long period);
我知道我可以在ScheduledFuture上调用cancel()来停止执行重复任务。但我想根据执行任务的结果取消定期计划任务,并且不确定如何做到最好。
ScheduledFuture是否允许我访问每个执行任务的结果?或者我是否需要某种可以保留对此ScheduledFuture的引用的任务侦听器,并以这种方式取消它?或者是其他东西?
好吧看起来有可能,但可能有更好的方法。
由于重复作业只接受Runnable(具有void返回类型),因此无法返回任务结果。因此,停止重复任务的唯一方法是使任务执行副作用,例如:向队列添加停止消息。然后一个单独的线程需要监视此队列,并且一旦看到该消息就可以取消该作业。
非常凌乱和复杂。
更好的选择是创建正常(一次)计划任务。然后,任务本身可以决定是否需要安排另一个任务,并且可以自己安排下一个任务。
保持句柄或原始固定费率ScheduledFuture
,然后当您想要取消它的条件出现时,安排一个取消的新任务。
你也可以用RunnableScheduledFuture
做点什么。
来自ScheduledExecutorService
文档
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html
import static java.util.concurrent.TimeUnit.*;
class BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep"); }
};
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
scheduler.schedule(new Runnable() {
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}
}
这是一个修改过的蜂鸣器示例,演示了如何在EACH计划任务之后做出决定。我使用了一个锁存器,所以我可以将它包装在一个测试用例中并断言发生了正确的事情(当然也是为了防止测试运行者的线程停止)。我也更改了间隔(在最初的10ms延迟后每隔10ms发出一次哔声),因此可以在一秒钟内复制,粘贴和执行测试,而不是一小时。
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class BeeperTest {
class BeeperControl {
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1, (runnable) -> {
Thread thread = new Thread(runnable);
thread.setName("MyAwesomeBeeperTestThread");
thread.setDaemon(true);
return thread;
});
public void beepTheNumberOfTimesIWant(CountDownLatch latch) {
long initialDelay = 10;
long frequency = 10;
TimeUnit unit = TimeUnit.MILLISECONDS;
final int numberOfTimesToBeep = 5;
AtomicInteger numberOfTimesIveBeeped = new AtomicInteger(0);
final ScheduledFuture[] beeperHandle = new ScheduledFuture[1];
beeperHandle[0] = scheduler.scheduleAtFixedRate(() -> {
if (numberOfTimesToBeep == numberOfTimesIveBeeped.get()) {
System.out.println("Let's get this done!");
latch.countDown();
beeperHandle[0].cancel(false);
}
else {
System.out.println("beep");
numberOfTimesIveBeeped.incrementAndGet();
}
}, initialDelay, frequency, unit);
}
}
@Test
public void beepPlease() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
BeeperControl control = new BeeperControl();
control.beepTheNumberOfTimesIWant(latch);
boolean completed = latch.await(1, TimeUnit.SECONDS);
Assert.assertTrue("Beeper should be able to finish beeping" +
"within allotted await time.", completed);
}
}