是handler.removeCallbacks(可运行)的equivalant到thred的退出 [英] Is handler.removeCallbacks(runnable) is equivalant to thred's exit
问题描述
说我写的:
Runnable runnable = new Runnable(){
@Override
public void run() {
Log.e(TAG, "runnable entered");
// working
Log.e(TAG, "runnable completed");
}
};
Thread t = new Thread(runnable);
t.start();
在 t.start();
我们将得到日志信息的可运行进入。
虽然将工作线程我总是想,我的可运行得到完成和放大器;完成后,我的应用程序将不再在此线程工作。
即我应该总是让这篇日志的可运行完成
After t.start();
we will get the log message runnable entered.
While working will threads I always want that my runnable gets completed & after completion my application will not anymore work on this thread.
i.e. I should always get this log runnable completed;
现在假设我用,而不是Thread类的处理程序。
Now suppose I use Handler instead of Thread class.
Handler handler = new Handler();
handler.post(runnable);
我可以肯定的是, handler.removeCallbacks(可运行)
就像是可运行的完成,我的应用程序将无法在本可运行的工作了?
Can I be sure that the handler.removeCallbacks(runnable)
is like runnable is completed and my application will not work on this runnable anymore?
推荐答案
handler.removeCallbacks(可运行)
的挂起的Runnable
从消息队列。但不会停止当前运行Runnable接口。例如说你有一个Runnable A的
handler.removeCallbacks(runnable)
removes the pending Runnables
from the message queue. But doesn't stop the currently running Runnable. For example say you have a Runnable of A
class A implements Runnable {
public void run() {
handler.removeCallbacks(a);
Log.i("tag", "are start");
// Do work
Log.i("tag", "end");
}
}
A a = new A();
和你叫
handler.post(一);
所以,当第一个可运行的执行它会调用 removeCallbacks
方法。但在logcat中,你会看到输出将
So when the first runnable execute it will call the removeCallbacks
method. But in the logcat you will see that the output will be
启动
结束
而如果你没有在使用
方法,那么输出将是 removeCallbacks
运行
while if you don't use removeCallbacks
in the run
method then the output will be
启动
结束
启动
结束
这篇关于是handler.removeCallbacks(可运行)的equivalant到thred的退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!