Tob*_*sPC 4 java multithreading android runnable
嘿,我目前有一个我的Android应用程序的问题.我通过实现Excecutor接口启动了一个额外的线程:
class Flasher implements Executor {
Thread t;
public void execute(Runnable r) {
t = new Thread(r){
};
t.start();
}
}
Run Code Online (Sandbox Code Playgroud)
我像这样开始我的Runnable:
flasherThread.execute(flashRunnable);
Run Code Online (Sandbox Code Playgroud)
但是我怎么能阻止它呢?
好的,这只是非常基本的线程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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后
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
Run Code Online (Sandbox Code Playgroud)
编辑:
只是偶然发现了这篇关于如何阻止线程的非常有用的文章.
| 归档时间: |
|
| 查看次数: |
11879 次 |
| 最近记录: |