java 从在 ScheduledExecutorService 中运行的任务本身中停止周期性任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4909824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stop a periodic task from within the task itself running in a ScheduledExecutorService
提问by akarnokd
Is there a nice way to stop the repetition of task from within the task itself when running in a ScheduledExecutorService?
在 ScheduledExecutorService 中运行时,是否有一种很好的方法可以停止从任务本身内部重复任务?
Lets say, I have the following task:
可以说,我有以下任务:
Future<?> f = scheduledExecutor.scheduleAtFixedRate(new Runnable() {
int count = 0;
public void run() {
System.out.println(count++);
if (count == 10) {
// ??? cancel self
}
}
}, 1, 1, TimeUnit.SECONDS);
From outside, it is easy to cancel via f.cancel(), but how can I stop the repetition at the specified place? (Passing the Future through an AtomicReference is not safe, because there is a potential window when the scheduleAtFixedRate returns f late and the variable is set late too, and the task itself might already run, seeing a null in the reference.)
从外面看,通过 f.cancel() 很容易取消,但是如何在指定的地方停止重复?(通过 AtomicReference 传递 Future 是不安全的,因为当 scheduleAtFixedRate 返回 f 延迟并且变量设置也延迟时有一个潜在的窗口,并且任务本身可能已经运行,在引用中看到空值。)
采纳答案by Peter Lawrey
When a repeating task throws an Exception or Error, it is placed in the Future and the task is not repeated again. You can throw a RuntimeException or Error of your choice.
当重复任务抛出异常或错误时,它会被放置在 Future 中,并且不会再次重复该任务。您可以抛出您选择的 RuntimeException 或 Error。
回答by Reboot
Instead of using an anonymous inner class you can use a named class which can then have a property for the Future
object you get from the Executor
when you schedule a task.
您可以使用命名类而不是使用匿名内部类,该类可以为Future
您Executor
在安排任务时获取的对象提供一个属性。
abstract class FutureRunnable implements Runnable {
private Future<?> future;
/* Getter and Setter for future */
}
When you schedule a task you can then pass the Future
to the Runnable
.
当您安排任务时,您可以将 传递Future
给Runnable
.
FutureRunnable runnable = new FutureRunnable() {
public void run() {
if (/* abort condition */)
getFuture().cancel(false);
}
};
Future<?> future = executor.scheduleAtFixedRate(runnable, ...);
runnable.setFuture(future);
Maybe you will have to make sure, that the task is not executed before the Future
has been set, because otherwise you will get a NullPointerException
.
也许您必须确保Future
在设置之前不会执行任务,否则您将获得NullPointerException
.
回答by Alb
It seems like bad design for the Runnable to know anything about the executor it is running in, or to throw an error if reaching 10 is not an error state is a hack.
对于 Runnable 来说,了解它正在运行的执行程序的任何信息,或者如果达到 10 不是错误状态则抛出错误是一种黑客行为,这似乎是一种糟糕的设计。
Can you do the loop to 10 outside of the scheduling and execution? This may require using a non-scheduling executor as you'd be scheduling them manually yourself.
你能在调度和执行之外循环到 10 吗?这可能需要使用非调度执行器,因为您需要自己手动调度它们。
回答by bestsss
Here is another way, that's even Thread safe;
这是另一种方式,甚至是线程安全的;
final Future<?>[] f = {null};
f[0]= scheduledExecutor.scheduleAtFixedRate(new Runnable() {
int count = 0;
public void run() {
System.out.println(count++);
if (count == 10) {
Future<?> future;
while(null==(future = f[0])) Thread.yield();//prevent exceptionally bad thread scheduling
future.cancel(false);
return;
//cancel self
}
}
}, 1, 1, TimeUnit.SECONDS);
回答by Michael Wiles
Just saw this now... because I wanted to do the same thing... here is my solution, I suspect this is threadsafe.
现在才看到这个......因为我想做同样的事情......这是我的解决方案,我怀疑这是线程安全的。
First create a container for the Future:
首先为 Future 创建一个容器:
public static class Cancel {
private ScheduledFuture<?> future;
public synchronized void setFuture(ScheduledFuture<?> future) {
this.future = future;
}
public synchronized void stop() {
LOG.debug("cancelling {}", future);
future.cancel(false);
}
}
And then the future code:
然后是未来的代码:
final Cancel controller = new Cancel();
synchronized (controller) {
ScheduledFuture<?> future = scheduler.scheduleWithFixedDelay(() -> {
if (<CONTINUE RUNNING CONDITION) {
} else {
// STOP SCHEDULABLE FUTURE
controller.stop();
}
}, startTime, timeBetweenVisbilityChecks);
controller.setFuture(future);
}
}
So notice how the stop will not be callable until the future has been created and the future has been set on the controller.
因此,请注意在创建未来并在控制器上设置未来之前,停止将无法调用。
Bear in mind that the Runnable is the anomymous inner class and this will get run in a different thread altogether.
请记住,Runnable 是匿名的内部类,这将完全在不同的线程中运行。