java 在单独的线程中停止可运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13958774/
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 runnable in a separate Thread
提问by TobiasPC
Hey there i currently have a problem with my android app. I′m starting an extra thread via implementing the Excecutor Interface:
嘿,我目前的 android 应用程序有问题。我正在通过实现 Executor 接口启动一个额外的线程:
class Flasher implements Executor {
Thread t;
public void execute(Runnable r) {
t = new Thread(r){
};
t.start();
}
}
I start my Runnable like this:
我像这样启动我的 Runnable:
flasherThread.execute(flashRunnable);
but how can i stop it?
但我怎么能阻止呢?
回答by JimmyB
Ok, this is just the very basic threading 101, but let there be another example:
好的,这只是非常基本的线程 101,但让我们再举一个例子:
Old-school threading:
老式线程:
class MyTask implements Runnable {
public volatile boolean doTerminate;
public void run() {
while ( ! doTerminate ) {
// do some work, like:
on();
Thread.sleep(1000);
off();
Thread.sleep(1000);
}
}
}
then
然后
MyTask task = new MyTask();
Thread thread = new Thread( task );
thread.start();
// let task run for a while...
task.doTerminate = true;
// wait for task/thread to terminate:
thread.join();
// task and thread finished executing
Edit:
编辑:
Just stumbled upon this very informative Articleabout how to stop threads.
刚刚偶然发现了这篇关于如何停止线程的非常有用的文章。
回答by hoaz
Not sure that implementing Executor
is a good idea. I would rather use one of the executors Java provides. They allow you to control your Runnable
instance via Future
interface:
不确定实施Executor
是个好主意。我宁愿使用 Java 提供的执行程序之一。它们允许您Runnable
通过Future
界面控制您的实例:
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<?> future = executorService.submit(flashRunnable);
...
future.cancel(true);
Also make sure you free resources that ExecutorService
is consuming by calling executorService.shutdown()
when your program does not need asynchronous execution anymore.
还要确保在您的程序不再需要异步执行时ExecutorService
通过调用来释放正在消耗的资源executorService.shutdown()
。
回答by tom
Instead of implementing your own Executor, you should look at ExecutorService. ExecutorService has a shutdown method which:
您应该查看 ExecutorService ,而不是实现您自己的Executor。ExecutorService 有一个关闭方法,它:
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.
启动有序关闭,其中执行先前提交的任务,但不会接受新任务。
回答by pkran
I would suggest to use the ExecutorService, along with the Furure object, which gives you control over the thread that is being created by the executor. Like the following example
我建议使用 ExecutorService 和 Furure 对象,它使您可以控制由执行程序创建的线程。像下面的例子
ExecutorService executor = Executors.newSingleThreadExecutor();
Future future = executor.submit(runnabale);
try {
future.get(2, TimeUnit.SECONDS);
} catch (TimeoutException ex) {
Log.warn("Time out expired");
} finally {
if(!future.isDone()&&(!future.isCancelled()))
future.cancel(true);
executor.shutdownNow();
}
This code says that the runnable will be forced to terminate after 2 seconds. Of course, you can handle your Future ojbect as you wish and terminate it according to your requierements
此代码表示可运行对象将在 2 秒后强制终止。当然,您可以随意处理您的 Future 对象并根据您的要求终止它