java ScheduledThreadPoolExecutor,如何停止可运行类JAVA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6774816/
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
ScheduledThreadPoolExecutor, how stop runnable class JAVA
提问by Khozzy
I have written the following code:
我编写了以下代码:
import java.util.Calendar;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
class Voter {
public static void main(String[] args) {
ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(2);
stpe.scheduleAtFixedRate(new Shoot(), 0, 1, TimeUnit.SECONDS);
}
}
class Shoot implements Runnable {
Calendar deadline;
long endTime,currentTime;
public Shoot() {
deadline = Calendar.getInstance();
deadline.set(2011,6,21,12,18,00);
endTime = deadline.getTime().getTime();
}
public void work() {
currentTime = System.currentTimeMillis();
if (currentTime >= endTime) {
System.out.println("Got it!");
func();
}
}
public void run() {
work();
}
public void func() {
// function called when time matches
}
}
I would like to stop the ScheduledThreadPoolExecutor when the func() is called. There is no need for it to work futher! I think i should put the function func() inside Voter class, and than create some kind of callback. But maybe I can do it from within the Shoot class.
我想在调用 func() 时停止 ScheduledThreadPoolExecutor。没有必要让它继续工作!我认为我应该将函数 func() 放在 Voter 类中,而不是创建某种回调。但也许我可以在 Shoot 课上做到这一点。
How can I solve it properly?
我该如何正确解决?
回答by ggarciao
The ScheduledThreadPoolExecutor
allows you to execute your task right away or schedule it to be executed later (you can set periodical executions also).
将ScheduledThreadPoolExecutor
允许你执行你的任务就可以安排在稍后执行(你可以设置定期执行也)。
So, if you will use this class to stop your task execution keep in mind this:
所以,如果你将使用这个类来停止你的任务执行,请记住这一点:
- There is no way to guarantee that one thread will stop his execution. Check the Thread.interrupt() documentation.
- The method
ScheduledThreadPoolExecutor.shutdown()
will set as canceled your task, and it will not try to interrupt your threads. With this method you actually avoid the execution of newer tasks, and the execution of scheduled but not started tasks. - The method
ScheduledThreadPoolExecutor.shutdownNow()
will interrupt the threads, but as I said in the first point of this list ...
- 没有办法保证一个线程会停止他的执行。检查 Thread.interrupt() 文档。
- 该方法
ScheduledThreadPoolExecutor.shutdown()
将设置为已取消您的任务,并且不会尝试中断您的线程。使用这种方法,您实际上可以避免执行较新的任务,以及执行已计划但未启动的任务。 - 该方法
ScheduledThreadPoolExecutor.shutdownNow()
将中断线程,但正如我在此列表的第一点中所说的......
When you want to stop the scheduler you must do something like this:
当您想停止调度程序时,您必须执行以下操作:
//Cancel scheduled but not started task, and avoid new ones
myScheduler.shutdown();
//Wait for the running tasks
myScheduler.awaitTermination(30, TimeUnit.SECONDS);
//Interrupt the threads and shutdown the scheduler
myScheduler.shutdownNow();
But what if you need to stop only one task?
但是如果您只需要停止一项任务怎么办?
The method ScheduledThreadPoolExecutor.schedule(...)
returns a ScheduleFuture
that is a representation of your already scheduled task. So, you can call the ScheduleFuture.cancel(boolean mayInterruptIfRunning)
method to cancel your task and try to interrupt it if you need to.
该方法ScheduledThreadPoolExecutor.schedule(...)
返回一个ScheduleFuture
代表您已经安排好的任务。因此,您可以调用该ScheduleFuture.cancel(boolean mayInterruptIfRunning)
方法来取消您的任务,并在需要时尝试中断它。