嘿,我目前的 Android 应用程序有问题。我通过开始一个额外的线程 实现执行器接口(interface):
class Flasher implements Executor {
Thread t;
public void execute(Runnable r) {
t = new Thread(r){
};
t.start();
}
}
我这样启动我的 Runnable:
flasherThread.execute(flashRunnable);
但是我怎样才能阻止它呢?
最佳答案
好的,这只是最基本的线程 101,但还有另一个例子:
老式线程:
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);
}
}
}
然后
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
编辑:
刚刚偶然发现了这个非常有用的信息 Article关于如何停止线程。
关于java - 在单独的线程中停止可运行的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13958774/