1
//case 1:post to main thread's handler
mHandler.post(new Runnable(){
public void run(){
dosomething(); //update UI
});
//case 2:run in main thread
dosomething();
//case 1:post to main thread's handler
mHandler.post(new Runnable(){
public void run(){
dosomething(); //update UI
});
//case 2:run in main thread
dosomething();
即使您在处理程序的线程中,情况1也会排队,情况2会立即执行。
那么,如果你在主线程,那么你在主线程,所以这很好。
但很多时候,你在一个工作线程中,想做一些只能在主线程中完成的事情(比如更新UI)。在这些情况下,你需要做一些特别的事情来在主线程上运行你的代码 - 你发布的处理程序代码就是一个例子。